只是有时,我会得到_pickle.UnpicklingError:pickle数据被截断



我偶尔会遇到一些Unpickling错误,但很多时候,它都很好。本质上,我在服务器端生成图像,并使用pickle将它们传输到客户端。

从本质上讲,我使用send((函数让客户端知道缓存数据的字节数,因此当我使用conn.send(graph(时,它可以使用rscSock.recv((,字节数为+1,以防止发生这种情况。而且它在大多数情况下都有效。偶尔我会被截断泡菜,我似乎不知道为什么。我尝试使用while循环从这里找到的代码中以4096块的形式接收它(python 3.6套接字pickle数据被截断(,但它挂在recv上。不知道该怎么办。

服务器代码:

elif cmd['cmd'] == 'RSC_VIEW_GRAPHS':
graphs = pickle.dumps(genGraphs(userSession['uid'], cmd['arg0'], cmd['arg1']))
send(conn, 'RSC_IMG_DATA', len(graphs))
conn.send(graphs)
del graphs

客户代码

send(rscSock, 'RSC_VIEW_GRAPHS', radioVar.get(), str(dateObj.date()))
resp = receive(rscSock)
if resp['resp'] == 'RSC_IMG_DATA':
graphs = pickle.loads(rscSock.recv(int(resp['arg0'])+1))

genGraphs((函数实现的图形变量返回BytesIO对象的数组,如genGraphs((函数末尾所示:

imgs = []
for x in statDict:
# Filler Code removed, irrelevant to post
imgs.append(io.BytesIO())
plt.savefig(imgs[-1], format='png')
plt.close()
return imgs

最后,这里是客户端和服务器的send((和receive((函数:

客户端

def send(conn, cmd, *argv): 
try:
cmdObj = {'cmd': cmd}

y = 0
for x in argv:
cmdObj['arg'+str(y)] = x
y+=1

cmdObj['key'] = sessionKey

obj = str.encode(json.dumps(cmdObj))
objLen = str(len(obj)).encode()

if conn.send(objLen):
if conn.recv(12).decode() == "RSC_LEN_OK":
if conn.send(obj):
if debug == 1: print("Sending '", obj, "' with length '", objLen ,"'")
return True
return False
except (ConnectionResetError, ConnectionAbortedError):
if cmdObj['arg1'] == 0:
return True
else:
messagebox.showerror("Real Estate Stat Counter", "Lost server connection. Please log back in.")
return False
def receive(conn):
try:
dataSize = int(conn.recv(8))
if dataSize < 16384:
conn.send(str.encode("RSC_LEN_OK"))
data = json.loads(conn.recv(dataSize).decode())
if debug == 1: print("Received '", data, "' with length '", dataSize ,"'")
return data
else:
conn.send(str.encode("RSC_LEN_NO"))
return False
except (OSError, UnicodeDecodeError, json.decoder.JSONDecodeError) as e:
return False

服务器

def send(conn, resp, *argv):
try:
respObj = {'resp': resp}

y = 0
for x in argv:
respObj["arg"+str(y)] = x
y+=1

obj = str.encode(json.dumps(respObj))
objLen = str(len(obj)).encode()

if conn.send(objLen):
if conn.recv(12).decode() == "RSC_LEN_OK":
if conn.send(obj):
if debug == 1: print("Sending '", obj, "' with length '", objLen ,"'")
return True
logging.log("WARN: send() failure")
return False
except ConnectionResetError:
logging.log("INFO: Client connection lost, terming socket")
conn.close()
return False
def receive(conn):
try:
dataSize = int(conn.recv(8))
if dataSize < 16384:
conn.send(str.encode("RSC_LEN_OK"))
data = json.loads(conn.recv(dataSize).decode())
if debug == 1: print("Received '", data, "' with length '", dataSize ,"'")
return data
else:
conn.send(str.encode("RSC_LEN_NO"))
return False
except (OSError, UnicodeDecodeError, json.decoder.JSONDecodeError) as e:
logging.log("WARN: receive() received raw data:", conn.recv(16384).decode())
logging.log("WARN: receive() exception:", e)
return False
except ValueError:
logging.log("WARN: receive() did not get a valid byte length first")
return False

因此,根据jasonharper的评论,我最终只修改了客户端代码:

data = []
while len(b"".join(data)) < int(resp['arg0']):
data.append(rscSock.recv(4096))
graphs = pickle.loads(b"".join(data))

现在,它将在循环中检查是否收到了在发送BytesIO对象数组之前最初告知的字节数。

到目前为止工作良好!!

相关内容

  • 没有找到相关文章

最新更新