pythonimport socketdef port_scan(target_host, target_ports):
for port in target_ports:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((target_host, port))
if result == 0:
print(f"Port {port} is open")
sock.close()
except KeyboardInterrupt:
print("Scan stopped by user")
break
except socket.gaierror:
print("Hostname could not be resolved")
break
except socket.error:
print("Could not connect to server")
breaktarget_host = "127.0.0.1"target_ports = [21, 22, 80, 443, 3389]port_scan(target_host, target_ports)