serialization - Passing data over sockets using pickle and variable length -
this code using. able send serialized data using pickle. have been tinkering few hours no luck. perhaps if here has better networking experience, quick insight appreciated don't have rewrite everything, think it's confusing already.
import select def send(sdef, data, slen): sdef.setblocking(0) sdef.sendall(str(len(str(data))).encode("utf-8").zfill(slen)) sdef.sendall(str(data).encode("utf-8")) def receive(sdef, slen): sdef.setblocking(0) ready = select.select([sdef], [], [], 60) if ready[0]: data = int(sdef.recv(slen)) # receive length # print "to receive: "+str(data) else: raise runtimeerror("socket timeout") chunks = [] bytes_recd = 0 while bytes_recd < data: ready = select.select([sdef], [], [], 60) if ready[0]: chunk = sdef.recv(min(data - bytes_recd, 2048)) if chunk == b'': raise runtimeerror("socket connection broken") chunks.append(chunk) bytes_recd = bytes_recd + len(chunk) else: raise runtimeerror("socket timeout") segments = b''.join(chunks).decode("utf-8") # print "received segments: "+str(segments) return segments
simplest implementation:
import select, pickle def send(sdef, data, slen): sdef.setblocking(0) sdef.sendall(str(len(str(pickle.dumps(data)))).encode("utf-8").zfill(slen)) sdef.sendall(str(pickle.dumps(data)).encode("utf-8")) def receive(sdef, slen): sdef.setblocking(0) ready = select.select([sdef], [], [], 60) if ready[0]: data = int(sdef.recv(slen)) # receive length # print "to receive: "+str(data) else: raise runtimeerror("socket timeout") chunks = [] bytes_recd = 0 while bytes_recd < data: ready = select.select([sdef], [], [], 60) if ready[0]: chunk = sdef.recv(min(data - bytes_recd, 2048)) if chunk == b'': raise runtimeerror("socket connection broken") chunks.append(chunk) bytes_recd = bytes_recd + len(chunk) else: raise runtimeerror("socket timeout") segments = b''.join(chunks).decode("utf-8") # print "received segments: "+str(segments) return segments
and cannot deserialize "segments" anymore, because string
this way it, literaleval should work under circumstances on serialized data
pickle.loads(ast.literal_eval(segments))
Comments
Post a Comment