I tried coding a small port scanner but thought I could fix the slow scanning by setting a timeout on closed ports... But that isn't working in this case.
This is my function for scanning the port:
def connHost(THost, TPort, output=None):
try:
socket.settimeout(3)
socket.connect((THost,TPort))
print colored(' [+]%d/tcp open', 'green') %TPort
if output != None:
file = open(output, 'a')
file.write("[+]Port: %d is open\n" %TPort)
file.close()
except:
print colored(' [-]%d/tcp close', 'red') %TPort
finally:
socket.close()
By the way, the output
variable has no problem, its a feature to output the open ports in a newly created file.
So whenever I run the script, it shows me this error:
If you want to view the full source, here is a link to my laggy scanner (without setting timeout feature): Port Scanner
Thank you