network programming: how does s.accept() work?

7stud bbxx789_05ss at yahoo.com
Mon Feb 25 06:20:43 EST 2008


On Feb 25, 2:43 am, bock... at virgilio.it wrote:
>
> by reusing the same variables without storing the previous values.
> This could make the Python
> garbage collector to attempt freeing the socket object created with
> the first connection, therefore
> closing the connection.
>
> If I'm right, your program should work as you expect if you for
> instance collect in a list the sockets
> returned by accept.
>

Yes, you are right about that.  This code prevents the first client
from disconnecting:

newsocks = []
client_addys = []

while 1:
    newsock, client_addr = s.accept()
    newsocks.append(newsock)
    client_addys.append(client_addr)

    print "original socket:", s.getsockname()

    print "new socket, self:", newsock.getsockname()
    print "new socket, peer:", newsock.getpeername()
    print



More information about the Python-list mailing list