Python sending dictionary through TCP

Pickle is considered insecure for sending data structures across connections as the object can never be trustfully reconstructed. This is why yaml, json or any other format is considered preferable.


You can use pickle to convert any Python object (including a dictionary) to a byte stream, which can then be sent over TCP and un-pickled on the receiving end.

Alternatively, you can use json, which isn't dependent on the receiving end being a Python client.


You should serialize it with pickle:

import pickle
dict = {...}
tcp_send(pickle.dumps(dict))

And on the other end:

import pickle
dict = pickle.loads(tcp_recieve())

If the other end is not written in python, you can use a data serialization format, like xml, json or yaml.