I'm a python beginner, and I'm curious how can I send a dictionary through TCP
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With