Python Forum

Full Version: Cannot unpickle tcp data? unpickling stack underflow
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
Well the data will vary, so no it could be 100 bytes or 200 bytes.
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.
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