Skip to content

Instantly share code, notes, and snippets.

@tai2
Last active December 16, 2015 02:39
Show Gist options
  • Select an option

  • Save tai2/5363973 to your computer and use it in GitHub Desktop.

Select an option

Save tai2/5363973 to your computer and use it in GitHub Desktop.
Test whether a host and port pair is connectable.
import sys
import socket
if __name__ == '__main__':
host = sys.argv[1]
port = sys.argv[2]
timeout = sys.argv[3] if 4 <= len(sys.argv) else 30
s = socket.socket(socket.AF_INET)
s.settimeout(int(timeout))
try:
s.connect((host, int(port)))
print 'connected'
s.close()
except:
print 'failed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment