0

I want to send a pickled, encoded version of an object Account across to my server, and then decoding it at the server end and reinstating it as the object with corresponding data, however I am unsure how to convert it back from a string to the bytes (?) data type as an object.

On the clients end, this is essentially what happens:

command = 'insert account'
tempAccount = Account('Isabel', 'password')
pickledAcc = pickle.dumps(tempAccount)

clientCommand = f"{command},{pickledAcc}".encode('utf-8')
client.send(clientCommand)

However on the servers side, it receives an empty string as the pickledAcc part.

I have simplified my code a bit but the I think the essentials are there, but if you need more I can give it lol. Also should mention that I have used the proper length etiquette, i.e. sending a message before this to notify the server how long this message will be. And all of my server infrastructure works properly :)

Basically, I just need to know if it is possible to encode the pickled Account object to send it, or if doing so will not ever work and is stupid to do so.

1
  • You shouldn't do it as pickled data may be incompatible with receiver's python version. You should consider python interpreter agnostic serialization method. Commented Jun 10, 2020 at 19:43

2 Answers 2

1

The problem with the format line is that you insert the __repr__ of the pickledAcc instead of the real bytes. This will not get you the wanted result:

for example:

command = "test"
pickledAcc = pickle.dumps("test_data")
clientCommand  = f"{command},{pickledAcc}".encode('utf-8')

Now client command will output:

b"test,b'\\x80\\x03X\\t\\x00\\x00\\x00test_dataq\\x00.'"

as you can see, the representation of the bytes array was encoded to utf-8 ("b\...")

To solve this problem I suggest you will convert the command to bytes array and then send clientCommand as a bytes array instead

hope that helped

Sign up to request clarification or add additional context in comments.

Comments

1

Client side:

import base64

##--snip--##

pickledAcc = base64.b64encode(pickledAcc).decode()

clientCommand = f"{command},{pickledAcc}".encode('utf-8')
client.send(clientCommand)

Server Side:

import base64

##--snip--##

pickledAcc = base64.b64decode(pickledAcc)
pickledAcc = pickle.loads(pickledAcc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.