Skip to content

Instantly share code, notes, and snippets.

@d6e
Created September 8, 2016 20:37
Show Gist options
  • Save d6e/acdcb390dc24d87c1392bd5a371588c1 to your computer and use it in GitHub Desktop.
Save d6e/acdcb390dc24d87c1392bd5a371588c1 to your computer and use it in GitHub Desktop.
Checks that a given tcp port is open.
import socket
import sys
def tcp_check(host, port=22):
ClientSocket = socket.socket()
try:
ClientSocket.connect((host, int(port)))
return_flag = True
except socket.error:
return_flag = False
finally:
ClientSocket.close()
return return_flag
host = sys.argv[1] if len(sys.argv) == 3 else 'localhost'
port = sys.argv[2] if len(sys.argv) == 3 else 22
print "using host: {} with port: {}".format(host, port)
print tcp_check(host, port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment