tcp

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Mar 2 22:56:55 EST 2008


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. The loop should start with the
accept call, and end at the conn.close() call (s.close() is left out
of the loop).
And then you get a pretty standard server that handles one connection
at a time.

--
Gabriel Genellina



More information about the Python-list mailing list