Reputation: 128
I'm working on a simple asynchronous HTTP proxy. i wrote this function to receive all data (since i don't know the specific size of received data):
def recvall(sock):
data = b""
while True:
r, w, e = select.select([sock], [], [], 1)
if r:
data+= sock.recv(65535)
if not data: # if empty
break
else:
break
return data
this piece of code works fine, but during my tests, i noticed that the loop never ends and keeps receiving the same data again and again (specifically, "301 moved permanently..."). This is very weird behavior of recv(). Probably this is not the best way to receive all data from sockets, but i can't think of any explanation for this behavior.
Upvotes: 1
Views: 715
Reputation: 369384
Following lines:
data += sock.recv(65535)
if not data: # if empty
break
should be changed:
chunk = sock.recv(65535)
if not chunk:
break
data += chunk
Otherwise it will check accumulated data, instead of the data just received; causing endless loop if there was any data received.
Upvotes: 2