How do I reconnect a disconnected socket?
Laszlo Nagy
gandalf at shopzeus.com
Fri Mar 28 11:03:39 EDT 2008
> This is what I've got right now:
>
> #! /usr/bin/env python
> import socket, string
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> def doconn():
> sock.connect(("localhost", 1234))
> def dodiscon():
> sock.close()
> doconn()
>
> doconn()
>
> while (1):
> buffer = sock.recv(1024)
> if not buffer:
> dodiscon()
>
sock.recv(1024) can return zero bytes of data indicating that no data
arrived yet. It does not mean that you have been disconnected. This is
especially true when you do nothing but recv, recv, recv() in an
infinite loop.
I recommend that you use select.select to see if there is some data that
can be read. Call socket.recv() only when you know that it will not fail.
Best,
Laszlo
More information about the Python-list
mailing list