0

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: Error socket.settimeout(3)

If you want to view the full source, here is a link to my laggy scanner (without setting timeout feature): Port Scanner

Thank you

Almog
  • 452
  • 1
  • 7
  • 13
param373r
  • 41
  • 1
  • 9

1 Answers1

0

Taking a look into the source code on GitHub, you aren't using a class - that is why the methods I linked in the comments did not work.

You are most likely getting this error because you are calling settimeout() on a reference and not an instance - you never instantiate the variable "socket"

s = # initialize socket here
s.settimeout(3)

You can find more information on bound and unbound methods here

Another note: From the docs, it is generally not conventional to import an entire module using * as

"...it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined."

gmdev
  • 2,725
  • 2
  • 13
  • 28
  • no i did create a reference in the final upload i made... but when I added timeout, with reference it gave me more error... So I directly called socket on each command... also, I used this technique after reading an answer from stackoverflow. So, am pretty much sure about it. – param373r Oct 11 '20 at 00:54
  • Okay, just curious - the error isn't caught by the `try except`? – gmdev Oct 11 '20 at 12:27
  • No, it did... The screenshot I took is after removing the try except blocks... Just to debug and see where it is catching error. – param373r Oct 13 '20 at 02:37
  • Can you update your question with the code that throws the error including the code where you instantiate the socket? – gmdev Oct 13 '20 at 02:45