Check port
A port is a
number associated with a specific service.

import socket
def is_open(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
res = s.connect_ex((host, port))
if res == 0:
print("%s %s open" % (host, port))
else:
print("%s %s closed" % (host, port))
is_open('python.org', 80)
is_open('python.org', 8080)
host = input('Host: ')
port = input('Port: ')
is_open(host, int(port))
Open ports
We can see which ports are open for
our system.

import socket, threading
def scan_port(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
res = s.connect(("127.0.0.1", port))
print('Port %s is open' % port)
except ConnectionRefusedError:
pass
for i in range(10, 100):
thread = threading.Thread(target=scan_port, args=[i])
thread.start()