socket programming and port scans

Peter Hansen peter at engcorp.com
Thu May 19 09:52:39 EDT 2005


rbt wrote:
> I don't fully understand sockets, I just know enough to be dangerous. 
> The below is not detected by nmap, but is affected by iptables or ipsec. 
> Can anyone explain why that is?
> 
>     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>     s.bind((ip_param, port_param))
>     while 1:
>         s.listen(5)

This, by the way, is wrong.  s.listen(5) just tells the stack that you 
would like to allow a backlog of up to 5 waiting-to-be-connected 
connection attempts while you are accepting another.  The call doesn't 
block and needn't be called repeatedly.  You could just as well do the 
call to listen first, then have an empty "while 1: pass" loop (but note 
that in either case it is a "busy wait", consuming 100% CPU while it runs).

What you are looking for is more like this:

s.listen(5)
while 1:
     s.accept()  # wait for connection, and ignore it


If you want to simulate a little server to allow multiple connections, 
you would of course need to use the value returned by accept() and 
probably call close() on the client socket right away.

-Peter



More information about the Python-list mailing list