Getting Pickle Data Truncated Error While Working With Sockets And Python
so here the problem. I am trying to send a large amount of binary image code over sockets which is the pickled data of the OpenCV image. I am resizing my image and then sending it
Solution 1:
.recv(n)
receives upto n bytes of data, but may (and likely will) return less. Check the length of frame_data
received and if less than n
issue another .recv
for the remainder, until you have all the bytes.
Also look into socket.makefile
which wraps the socket in a file-like object, and you can call .read(n)
to read exactly n bytes.
Full-example of two way pickling of objects between client/server:
from socket import *
import threading
import pickle
classObjectStream:
def__init__(self, sock):
self.sock = sock
self.writer = sock.makefile('wb')
self.reader = sock.makefile('rb')
# Objects are sent/received as a 4-byte big-endian integer of# the pickled object data length, followed by the pickled data.defget_obj(self):
header = self.reader.read(4)
ifnot header:
returnNone
length = int.from_bytes(header,'big')
return pickle.loads(self.reader.read(length))
defput_obj(self,obj):
data = pickle.dumps(obj)
header = len(data).to_bytes(4,'big')
self.writer.write(header)
self.writer.write(data)
self.writer.flush() # important!defclose(self):
if self.sock isnotNone:
self.writer.close()
self.reader.close()
self.sock.close()
self.sock = None
self.writer = None
self.reader = None# Support for 'with' to close everything.def__enter__(self):
return self
def__exit__(self,*args):
self.close()
# Support for no more references to ObjectStreamdef__del__(self):
self.close()
defserver():
s = socket()
s.bind(('',9999))
s.listen()
with s:
while run:
c,a = s.accept()
print('server: connect from',a)
with ObjectStream(c) as stream:
whileTrue:
obj = stream.get_obj()
print('server:',obj)
if obj isNone: breakifisinstance(obj,list):
# reverse lists
stream.put_obj(obj[::-1])
elifisinstance(obj,dict):
# swap key/value in dictionaries
stream.put_obj({v:k for k,v in obj.items()})
else:
# otherwise, echo back same object
stream.put_obj(obj)
print('server: disconnect from',a)
defclient():
s = socket()
s.connect(('localhost',9999))
with ObjectStream(s) as stream:
stream.put_obj([1,2,3])
print('client:',stream.get_obj())
stream.put_obj({1:2,3:4,5:6})
print('client:',stream.get_obj())
stream.put_obj(12345)
print('client:',stream.get_obj())
run = True# Simple global flag to control server Thread
threading.Thread(target=server).start()
client() # Server can handle only one client at a time as written.
client() # But can serially serve clients.
run = False
Output:
server: connect from ('127.0.0.1', 52689)
server: [1, 2, 3]
client: [3, 2, 1]
server: {1: 2, 3: 4, 5: 6}
client: {2: 1, 4: 3, 6: 5}
server: 12345
client: 12345
server: None
server: disconnect from ('127.0.0.1', 52689)
server: connect from ('127.0.0.1', 52690)
server: [1, 2, 3]
client: [3, 2, 1]
server: {1: 2, 3: 4, 5: 6}
client: {2: 1, 4: 3, 6: 5}
server: 12345
client: 12345
server: None
server: disconnect from ('127.0.0.1', 52690)
Post a Comment for "Getting Pickle Data Truncated Error While Working With Sockets And Python"