Re: Socket recv(1) seems to block instead of returning end of file.
- From: Grant Edwards <grante@xxxxxxxx>
- Date: Thu, 23 Aug 2007 14:03:52 -0000
On 2007-08-23, Hendrik van Rooyen <mail@xxxxxxxxxxxxxxx> wrote:
While doing a netstring implementation I noticed that if you
build a record up using socket's recv(1), then when you close
the remote end down, the recv(1) hangs,
I don't see that behavior running 2.4 on Gentoo.
despite having a short time out of 0.1 set.
What time out? A socket's recv method doesn't do timeouts.
If however, you try to receive more than one char, (I tested
with 3, did not try 2), then when you shut the remote end down
you do not get a time out, but an empty string - the normal
end of file, I suppose.
Has anybody else seen this behaviour?
No. recv(1) works fine for me (Python 2.4 under Gentoo).
Perhaps you could post a minimal example that doesn't work for
you?
------------------------------------------------------------------------
#!/usr/bin/python
#reader
import socket
HOST = '' # Symbolic name meaning the local host
PORT = 8765 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1)
print "rx:",len(data)
if not data: break
conn.close()
------------------------------------------------------------------------
------------------------------------------------------------------------
#!/usr/bin/python
# writer
import socket,random
HOST = '' # Symbolic name meaning the local host
PORT = 8765 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print 'Connected to',((HOST,PORT))
for i in range(10):
data = "abcdefghijklmnopqrstuvwxyz"[:random.randint(1,20)]
s.send(data)
print "tx:",len(data)
conn.close()
------------------------------------------------------------------------
--
Grant Edwards grante Yow! Loni Anderson's hair
at should be LEGALIZED!!
visi.com
.
- Follow-Ups:
- Re: Socket recv(1) seems to block instead of returning end of file.
- From: Hendrik van Rooyen
- Re: Socket recv(1) seems to block instead of returning end of file.
- From: Bryan Olson
- Re: Socket recv(1) seems to block instead of returning end of file.
- References:
- Socket recv(1) seems to block instead of returning end of file.
- From: Hendrik van Rooyen
- Socket recv(1) seems to block instead of returning end of file.
- Prev by Date: Python submodules and name imports
- Next by Date: Re: simple spider in python
- Previous by thread: Socket recv(1) seems to block instead of returning end of file.
- Next by thread: Re: Socket recv(1) seems to block instead of returning end of file.
- Index(es):