[英]How to send an object over network
I need to send an object from a client to a server, but sockets only let you send bytes. 我需要将一个对象从客户端发送到服务器,但是套接字只允许您发送字节。 How do I convert a custom object to bytes in Python 3.5 so I can send it via the socket?
如何在Python 3.5中将自定义对象转换为字节,以便可以通过套接字发送它?
Depends on what you are trying to send over the webs, if it is just some basic dict
or list
of basic types you could serialize it to json
. 取决于您尝试通过网络发送的内容,如果它只是一些基本
dict
或基本类型list
,则可以将其序列化为json
。 See here . 看这里 。
If you really want to serialize entire python class then use already mentioned pickle
, although it can be dangerous, see here . 如果您确实要序列化整个python类,请使用已经提到的
pickle
,尽管这样做很危险,请参见此处 。
And remember to NOT execute anything deserialized which came from the internet. 并且请记住不要执行任何来自互联网的反序列化操作。
您需要序列化您的对象,这可以通过pickle库来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.