问题描述:
使用pickle序列化和反序列化进行socket传输时,发送方可以正常执行,接收方报错:_pickle.UnpicklingError: pickle data was truncated
参考:https://blog.csdn.net/qq_42816922/article/details/110678845
通信双方代码:
@发送方
connection, address = sock.accept()
message = connection.recv(1024).decode()
if message == 'helper':
Helper_pickle = pickle.dumps(Helper)
helper = Helper_pickle
connection.sendall(helper)
connection.close()
@接收方
sock = socket.socket()
sock.connect((HOST, PORT))
message = 'helper'
sock.sendall(message.encode())
data = sock.recv(1024)
Helper = pickle.loads(data)
执行结果报错:_pickle.UnpicklingError: pickle data was truncated
原因分析
根据报错提示:数据被截断,反序列化出现错误。
实际上socket.recv的缓冲区是有限的,就算设置4096*4096也会导致长数据的截断。
接收方并没有接收到完整的数据,导致在进行反序列化时,pickle无法正确进行反序列化,进而出错。
解决方案
@接收方
sock = socket.socket()
sock.connect((HOST, PORT))
message = 'helper'
sock.sendall(message.encode())
data = b""
while True:
packet = client.recv(4096)
# print("1")
if packet is not None:
data += packet
if packet is None or len(packet) < 4096:
break
print("Download weights finished", len(data))
Helper = pickle.loads(data)