I've tried looking around online through different python docs, forums, and other people's questions but I haven't found anyone with this same question.
What my scripts typically look like is I'll create a socket connection that tries connecting to ports 1-9999 and will only tell me when a port is open, and when I run this on windows sometimes it can take 20 minutes to check all of the ports, but will always take a comparably long time. When I run the same scripts on linux, it'll cycle through all 9999 ports very quickly while still returning the desired results.
What's the difference here? Is there an argument I could pass with a function or a configuration I could make on windows to get the same speed? What I've noticed is on linux if a port is unresponsive you get an immediate response where with windows, it'll wait a good while for a response before saying the port is unresponsive depending on what ports it's trying to connect to. I've tried using the .settimeout() function for socket but it doesn't seem to have an effect on how long it will wait for a response before a connection is/isn't established and I don't get any exceptions raised.
Is linux just the better operating system when it comes to building network tools? I have both at my disposal so I don't mind using one over the other. I'd just like to know if there's anything that could be done to make port scanning almost as equally fast on both operating systems, otherwise I won't spend as much time building on windows.
The performance is consistently the same regardless of which network I'm on.
To be clear, my question is why is the performance so different on windows compared to linux when given the same functions, and is there anything that can be done to make port scanning with sockets faster like it is on linux?
here's the piece I use to check ports
def whole_scan(Host_): service = '' host = Host_ max_port = 9999 min_port = 1 def scan_host(host, port, r_code = 1): try: s = socket(AF_INET, SOCK_STREAM) code = s.connect_ex((host, port)) if code == 0: r_code = code s.close() except Exception, e: pass return r_code hostip = gethostbyname(host) for port in range(min_port, max_port): try: response = scan_host(host,port) if response == 0: try: service = getservbyport(port) except Exception, e: service = 'n/a' print(" |--port: %d\t%s" % (port,service.upper())) except Exception, e: pass
I've also verified my firewall is disabled and adding the value to my registry to disable the limit on connections had no change on performance. I'm on windows 10.