Python sending dictionary through TCP
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.
yes. its might be unsecured. but you can use https and send it thru post method. for get mehtod, try to consider encrypt pickle before sending
精彩评论