My problem is not really python specific but as I do my implementation
in python I hope someone here can help me.
I have two programs that talk through a socket. Here is the code :
<server>
sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
sock.setsockopt (socket.SOL_SOC KET, socket.SO_REUSE ADDR, 1)
sock.settimeout (5.0)
sock.bind(("", port)
# We only handle one connection
sock.listen(1)
while 1:
newsock, address = sock.accept()
handle(newsock, address)
def handle(sock, address) :
print "Connection from", address
dataReceived = newsock.recv(10 24)
while 1:
try:
newsock.send(da ta)
except socket.timeout, err:
print "Connection timeout %s" % err
break
except socket.error, err:
print "Connection broken %s" % err
break
</server>
<client>
sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
sock.settimeout (5.0)
sock.connect((h ost, port))
sock.send("Gimm e a piece of information, please\r\n")
while 1:
r, w, e = select.select([s], [], [], 1.0)
if r != []:
handle_connecti on(s)
def handle_connecti on(sock):
try:
response_data = sock.recv(1024)
except socket.timeout:
print "Socket timeout"
return
manage(response _data)
</client>
Ok I hope it's clear. My problem is that "select" only tests if there's
data on the socket, and not the state of the socket. I want to be able
to know if socket is "alive" or something like that. But I don't want
to make "send" in the client (stay passive). Then, if I know that my
socket is closed or my link down, I can try to reconnect periodically.
I would rewrite the while in the client like that :
<client>
while 1:
if not test_connect(s) :
reconnect(s)
# else continue normally
r, w, e = select.select([s], [], [], 1.0)
if r != []:
handle_connecti on(s)
</client
My env: python2.3, linux/win32.
Thanks in advance,
--
Thomas Herve <sorry for my english>
in python I hope someone here can help me.
I have two programs that talk through a socket. Here is the code :
<server>
sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
sock.setsockopt (socket.SOL_SOC KET, socket.SO_REUSE ADDR, 1)
sock.settimeout (5.0)
sock.bind(("", port)
# We only handle one connection
sock.listen(1)
while 1:
newsock, address = sock.accept()
handle(newsock, address)
def handle(sock, address) :
print "Connection from", address
dataReceived = newsock.recv(10 24)
while 1:
try:
newsock.send(da ta)
except socket.timeout, err:
print "Connection timeout %s" % err
break
except socket.error, err:
print "Connection broken %s" % err
break
</server>
<client>
sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
sock.settimeout (5.0)
sock.connect((h ost, port))
sock.send("Gimm e a piece of information, please\r\n")
while 1:
r, w, e = select.select([s], [], [], 1.0)
if r != []:
handle_connecti on(s)
def handle_connecti on(sock):
try:
response_data = sock.recv(1024)
except socket.timeout:
print "Socket timeout"
return
manage(response _data)
</client>
Ok I hope it's clear. My problem is that "select" only tests if there's
data on the socket, and not the state of the socket. I want to be able
to know if socket is "alive" or something like that. But I don't want
to make "send" in the client (stay passive). Then, if I know that my
socket is closed or my link down, I can try to reconnect periodically.
I would rewrite the while in the client like that :
<client>
while 1:
if not test_connect(s) :
reconnect(s)
# else continue normally
r, w, e = select.select([s], [], [], 1.0)
if r != []:
handle_connecti on(s)
</client
My env: python2.3, linux/win32.
Thanks in advance,
--
Thomas Herve <sorry for my english>
Comment