简体   繁体   English

在Python中通过网络发送对象的最佳方法是什么?

[英]What's the best way to send an object over a network in Python?

I need to send objects around a network. 我需要在网络周围发送对象。 I'm going to be using Twisted, and I've just started looking around the documentation for it. 我将使用Twisted,我刚刚开始查看文档。

As far as I know, the only way python implements sockets is through text. 据我所知,python实现套接字的唯一方法是通过文本。 So how would I send an object using strings? 那么我如何使用字符串发送对象? Pickle? 泡菜? Or is there something better? 还是有更好的东西?

The most general serialization on offer between Python end-points is the pickle format (in Python 2.any, be sure to use the cPickle module, and the -1 aka pickle.HIGHEST_PROTOCOL protocol; if you need interoperability between Python 2.any and Python 3.any more care is needed). Python端点之间提供的最常见的序列化是pickle格式(在Python 2.any中,一定要使用cPickle模块,以及-1 aka pickle.HIGHEST_PROTOCOL协议;如果你需要Python 2.any和Python之间的互操作性Python 3.需要更多的关注)。 For especially simple objects, the marshal module can sometimes be faster and more compact. 对于特别简单的物体, marshal模块有时可以更快,更紧凑。 For interoperation with non-Python endpoints, json may be best (or you could use xml to define or adopt other existing serialization formats), but those will likely be bulkier and slower to format and parse. 为了与非Python端点进行互操作, json可能是最好的(或者您可以使用xml来定义或采用其他现有的序列化格式),但这些格式和解析可能会更庞大,速度更慢。

As far as I know, the only way python implements sockets is through text. 据我所知,python实现套接字的唯一方法是通过文本。

Nope, all strings of bytes are welcome!-) You may be confused by the fact that in Python 2 a "normal string" is actually a string of bytes ("text" would be the unicode type); 不,所有字符串都是受欢迎的! - )你可能会感到困惑的是,在Python 2中,“普通字符串”实际上是一串字节(“text”将是unicode类型); Python 3 sets things right and uses Unicode for "normal strings" and a specific byte string type for strings of bytes. Python 3设置正确,并使用Unicode作为“普通字符串”,使用特定字节字符串类型作为字节串。

Strings of bytes are the general way in which any language will perform any form of serialization and deserialization, according to some protocol or other -- such byte streams or blobs can go into networks, databases, plain files, etc, etc, of course. 根据某些协议或其他语言,字节串是任何语言执行任何形式的序列化和反序列化的一般方式 - 当然,这样的字节流或blob可以进入网络,数据库,普通文件等。

Twisted offers its own serialization format, as part twisted.spread -- it's mostly for use with Perspective Broker (PB) but you could repurpose it for your own purposes if you don't want to use PB for some special reason. Twisted提供了自己的序列化格式,作为部分twisted.spread - 它主要用于Perspective Broker(PB),但如果你不想因某些特殊原因使用PB,你可以将它重新用于你自己的目的。 The docs for the serialization part, twisted.spread.jelly , are here , and the summarize well the format's goals...: 序列化部分的文档twisted.spread.jelly在这里 ,并总结了格式的目标......:

S-expression-based persistence of python objects. 基于S表达式的python对象持久化。

It does something very much like Pickle; 它做的非常像Pickle; however, pickle's main goal seems to be efficiency (both in space and time); 然而,泡菜的主要目标似乎是效率(空间和时间); jelly's main goals are security, human readability, and portability to other environments. jelly的主要目标是安全性,人类可读性以及对其他环境的可移植性。

If you care more about security, readability, and portability, than speed and compactness, then jelly might indeed serve you well. 如果您更关心安全性,可读性和便携性,而不是速度和紧凑性,那么果冻可能确实对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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