tcp

Dan Stromberg strombrg at gmail.com
Sat Mar 8 12:39:17 EST 2008


On Sun, 02 Mar 2008 23:05:27 -0500, Roy Smith wrote:

> In article
> <a983ed83-34cb-49f0-944c-e0600cbb3a2f at 2g2000hsn.googlegroups.com>,
>  Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
> 
>> On 2 mar, 17:21, castiro... at gmail.com wrote:
>> 
>> > This worked:
>> >
>> > import socket
>> > from time import time
>> >
>> > for i in range( 20 ):
>> >     HOST = ''
>> >     PORT = 80 #<----
>> >     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>> >     s.bind((HOST, PORT))
>> >     print( 'listen' )
>> >     s.listen(1)
>> >     conn, addr = s.accept()
>> >     print( 'connected', addr )
>> >     print( conn.recv( 4096 ) ) #<----
>> >     conn.send( bytes('<html><body>test %f</body></
>> > html>'%time(),'ascii') )
>> >     conn.close() #<----
>> >     s.close()
>> >
>> > ... and connect with a browser:  http://localhost/if it's internet
>> > exploder.
>> 
>> Note that there is no need (nor is desirable) to close and rebind the
>> listening socket for each connection.
> 
> I'd say, "nor is desirable", is an understatement.  On most systems, an
> attempt to re-bind to a given port number soon after it was unbound will
> fail (unless you utter magic ioctl incantations).  This will manifest
> itself in the s.bind() call raising an exception on the *second* pass
> through the loop.

I believe the incantation may be setsockopt(), not ioctl(), but if 
there's an ioctl() way of doing it, i'd be interested in seeing it.




More information about the Python-list mailing list