Check port
A port is a number associated with a specific service.
"""Using Python to check if remote port is open and accessible
connect_ex() return an error indicator ...
instead of raising an exception
"""
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: ') # localhost
port = input('Port: ') # 80
is_open(host, int(port))
Open ports
We can see which ports are open for our system.
"""Create instance of socket for every port
"""
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()
# Port 21 is open
# Port 25 is open
# Port 80 is open
Last update: 420 days ago