1

I am attempting to make a simple port scanner:

socket.setdefaulttimeout(1)

try:
    for port in range(lowport,highport):  
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      #s.settimeout(1)
      x = s.connect_ex((remoteServerIP, port))
      if x == 0:
        print "[+] Port {}:      Open".format(port)
        try:
          s.settimeout(7)
          s.send("blah")
          print s.recv(100)

My question is, will the socket timeout go back to the default of (1) after the 'if' statement completes (as I believe it should and is most python) or do I need to place it explicitly in the iteration 'for' each port as I have commented out inline above.. goal being a timeout of (1) to see if the port is open, but (7) to receive the banner..

1 Answer 1

1

From the docs of socket.setdefaulttimeout:

socket.setdefaulttimeout(timeout)

Set the default timeout in seconds (float) for new socket objects. A value of None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.

The timeout is a configurable parameter you set once at the beginning of the program, and it remains for the duration of execution until and unless you change it again, explicitly.

If a socket overrides the default by setting its own timeout, other sockets are not affected.

Sign up to request clarification or add additional context in comments.

9 Comments

Ok, I get that. But have I explicitly changed it for the entire program and all further port enquiries or have I just changed it for that instance where conn_ex==0.
@conma293 Unless I am missing something, you've changed it for good.
Gotcha thanks, will uncomment that initial explicit timeout, thought because I am calling on that particular socket instance which will get torn down afterwards that each new socket created will then revert back to the default timeout... but unsure...
@coldspeed thanks but im not satisfied just yet; so before anything gets created we change the default timeout of any created socked to (1), we then create a socket, it should then inherit the timeout of (1). If the port is open, it should then explicitly change its timeout to (7), importantly this is not calling the socket library to set a default timeout, but a call for this particular instance of a created socket setting a timeout of (7), after that loop is finished and torn down with s.close() it goes onto the next port.
@conma293 This is what I think. 1. You set the default for all sockets. 2. One socket can override the default. 3. Other sockets are not affected by the overriding socket. If you aren't convinced, feel free to just try it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.