Python Forum
Cannot unpickle tcp data? unpickling stack underflow
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot unpickle tcp data? unpickling stack underflow
#1
Hi,

Can anyone help me with this?

I am sending some data from Autoit to Python with TCP.
It is sent serialized from the Autoit client, so I have to unpickle it from my Python server:

more code ...
while True:
                
                data_received = self.clientsocket.recv(1024)
                print(data_received)
                data = pickle.loads(data_received)
                print(data)
... more code

I receive this byte stream from the client: b'a|0x737C3078364136463639364524617C307837333743333037383336333333363436333733353330343433303431#'
Python is giving me this error when trying to deserialize it:
Erreur TCP: unpickling stack underflow
Reply
#2
Do you know how much data the sender is going to transmit? If you send 100 bytes, that doesn't guarantee that the receiver will be able to read 100 bytes. It might only get 50 the first read and more later. But the unpickle needs all the data at once, not split up.
Reply
#3
Well the data will vary, so no it could be 100 bytes or 200 bytes.
Reply
#4
You can't feed it to pickle until you have all the data. You need to know you've got all the data somehow. TCP is a single stream, so it won't tell you that everything has arrived until you close the session and receive an EOF. You could send the size first, and then the receiving side could know how much data to expect. Then only send it to pickle after that much arrives.
Reply
#5
I wasn't very familiar with the pickle format, but it appears that a pickle stream always ends with a period (byte 46). If your data doesn't have a period, you don't have all the data yet (or it isn't pickle data).


>>> data = ([1,2,3], (4, 5, 6), {7:8, 9:10})
>>> pickle.dumps(data)[-1]
46
>>> pickle.dumps(data).count(46)
1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does unpickling only work ouside of a function? pjfarley3 5 3,364 Dec-24-2020, 08:31 AM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020