简体   繁体   English

如何通过网络发送对象

[英]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中将自定义对象转换为字节,以便可以通过套接字发送它?

You can serialize the object using pickle , which will convert it to bytes and allow you to send it. 您可以使用pickle序列化对象,该对象会将其转换为bytes并允许您发送它。 You can read up on how to use pickle here 您可以在这里阅读如何使用pickle

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM