socket.error delays

TheDustbustr thedustbustr at
Wed Sep 12 03:51:03 CEST 2001

I am writing a very simple portscanner for use on my windows box.  It works
well, by calling sock.connect() and if it succedes, tis open, if it falis, the
port is closed.

import sys
import socket

  host=sys.argv[1]     #host given through cmd line
except IndexError:
  host='localhost'       #default host

print 'PORT\tSTATE'
for n in range(30,100):    #ports to be scanned
    print '%s\topen'%n     #open
  except socket.error:
    print'%s\tclosed'%n    #closed

Problem is:  If the port is closed, it takes about 45 seconds for socket.error
to be raised!  If the port is open the result is instantanious.  Run off of
localhost, it takes about 3 sec to find a closed port and instantly finds an
open port.  How can i reduce the socket.error catching delay?  I dont want to
use a timeout because of lagged packets mesisng things up (and i dont want to
use threads if i can help it anyway).

Thanks, Dustin

More information about the Python-list mailing list