socket.error delays

TheDustbustr thedustbustr at aol.com
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.

#!/usr/env/python
import sys
import socket

try:
  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
  try:
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((host,n))
    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