Last active
October 20, 2019 18:04
-
-
Save doublenns/08345c5ff4b802dd14a46c2c03cb137c to your computer and use it in GitHub Desktop.
Check to see whether a port on a remote server is accessible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
def check_conn_to_remote_port(address, port): | |
s = socket.socket() | |
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(5) | |
print(f"Attempting to connect to {address} on port {port}") | |
try: | |
s.connect((address, port)) | |
print(f"Connected to {address} on port {port} -- success!") | |
#print "Connected to %s on port %s -- success!" % (address, port) | |
return True | |
except socket.error as err: | |
print(f"Connection to {address} on port {port} has FAILED: {err}") | |
#print "Connection to %s on port %s has FAILED: %s" % (address, port, err) | |
return False | |
finally: | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment