Skip to content

Instantly share code, notes, and snippets.

@alastairhm
Created April 12, 2022 07:51
Show Gist options
  • Save alastairhm/81ce159a0825068793a920fe4a88e235 to your computer and use it in GitHub Desktop.
Save alastairhm/81ce159a0825068793a920fe4a88e235 to your computer and use it in GitHub Desktop.
Check if port is open TCP/UDP in Python, zero result means connect is made.
def check_tcp(host,ip):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((host,ip))
print(result)
sock.close()
def check_udp(host,ip):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
result = sock.connect_ex((host,ip))
print(result)
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment