socket.connect method
import socket
def is_network_port_open(hostname, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((hostname, port))
sock.close()
return True
except socket.error:
return False
is_open = is_network_port_open('google.com', 80)
print(is_open) # True
is_open = is_network_port_open('127.0.0.1', 443)
print(is_open) # False
Leave a Comment
Cancel reply