tags:

views:

53

answers:

1

I am doing this in my code,

HOST = '192.168.1.3'    
PORT = 50007              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
query_details = {"page" : page, "query" : query, "type" : type}
s.send(str(query_details))
#data = eval(pickle.loads(s.recv(4096)))
data = s.recv(16384)

But I am continually getting EOF at the last line. The code I am sending with,

self.request.send(pickle.dumps(results))
+4  A: 

s.send is not guaranteed to send every byte you give it; use s.sendall instead.

Similarly, s.recv is not guaranteed to receive every byte you ask -- in that case you need to know by other ways exactly how many bytes you need to receive (e.g., send first the length of the string you're sending, encoded with the struct module) and you're responsible for doing the looping yourself to that purpose. There isn't and cannot be any recvall because stream sockets are not "self-delimiting" in any way -- they're just streams, broken up into totally arbitrary packets of sizes not semantically relevant.

You shouldn't ever get an EOF from the recv itself, though of course you can expect to get it in the line you've commented out, from the pickle.loads (because its argument may well be only a part of the bytes that the counterpart sent: as explained in the previous paragraph).

Alex Martelli