I am in middle of solving the port-scanner project.
But the tests had failed. Seems like my way of using socket with connect_ex method could not detect:
port 443
port 80
or maybe more
Any advice? or should i abandon socket module, and switch to nmap module ?
Move the close() into your loop. You need to open and close the connection for each port you try. Here’s it’s likely making the first connection but fails the rest since the socket is already in use.
From your advice, then i also make sure the socket is initialised and closed every loop.
Then it finally works!!!
import socket
open_ports = []
for i in range(first_port, last_port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.5)
if s.connect_ex((target, i)):
pass
else:
open_ports.append(i)
s.close()