使用 Twisted 的 Python Web 服务
这与我之前的问题Python Web服务有关。
我将使用 Tornado 在服务器和客户端之间交换信息。将有一台服务器和 N 个客户端。客户端将定期(每 2 分钟左右)发送信息(磁盘使用情况、进程等)。客户端的数据将由自定义类/列表表示。如果另一端(服务器)有相同的数据那就太好了。
我有使用 SOAP 的经验,这可能没问题(服务器上有足够的位超时),但宁愿使用更轻量和Pythonic 的东西。沟通或多或少只是客户端 ->服务器。
服务器端和客户端都是用Python编写的。
我应该在 Twisted 文档中寻找什么来执行此类操作?
编辑:我不是问如何序列化数据(JSON或pickle或XML等)。我想知道 Twisted 有哪些选项来传输数据。
对于 SOAP,我会有这样的方法:
- sendDiskUsage(DiskUsage class instance)
- sendProcesses(ProcessList class instance)
- etc..
我想知道 Twisted 有哪些选项。其中之一是 XML-RPC,这还可以,但它不是我最喜欢的...
edit2: 通信将是“双向”的 - 客户端将从服务器读取任务...
This is connected with my previous question Python web service.
I'll use Tornado to exchange information between server and clients. There will be one server and N clients. Clients will send information (disk usage, processes etc.) periodically (every 2 minutes or so). The data on client side will be represented by custom classes/lists. It would be nice to have the same data on the other side (server).
I have experience with SOAP and it would probably be ok for this (with enough bit timeouts on the server), but would rather use something more light and pythonic. The communication will be more or less only client -> server.
Both server and client side are writen in Python.
What should I look for in Twisted documentation for doing this kind of stuff?
edit: I'm not asking on how to serialize data (JSON or pickle or XML etc.). I would like to know what are the options of Twisted to transport data.
With SOAP I would have methods like this:
- sendDiskUsage(DiskUsage class instance)
- sendProcesses(ProcessList class instance)
- etc..
I would like to know what are the options with Twisted. One of them is XML-RPC which would be ok, but it's not my favourite...
edit2: the communication will be "two way" - client's will read tasks from server...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我推荐 AMP。这是一个非常简单的基于键值对的协议,非常适合您正在做的事情。透视经纪人是另一种选择..但它有点复杂,而且通常是不必要的。
AMP 直接通过 TCP 运行(为什么要费心使用 HTTP?),序列化格式既最小又合乎逻辑。从您可能的意思来看,这使得它变得“轻量”和“Pythonic”,但这些术语可以用几种不同的方式解释。
查看 Twisted 代码示例 上的 AMP 示例,它们非常自学解释性的。 AMP 连接是双向的,因此请尝试修改示例,以便服务器向客户端询问其当前的磁盘使用情况。
twisted.protocols.amp
API 文档在这里会很有用。当您准备好构建真正的应用程序时,请阅读 Twisted from Scratch 或手指的进化。
AMP 已在大多数流行语言中实现,但如果您正在寻找更“主流”的语言, protobuf 是 Google 的东西。 IMO 它过于复杂并且缺乏一些重要的功能,例如错误反馈。
I recommend AMP. It's a very simple key-value pair based protocol, ideal for what you're doing. Perspective broker is another alternative.. but it's slightly complicated, and usually unnecessary.
AMP runs directly over TCP (why bother with HTTP?), the serialization format is both minimal and logical. This makes it 'light' and 'pythonic' in the sense you probably mean, but those terms could be interpreted in a few different ways.
Take a look at the AMP examples on Twisted code examples, they're pretty self-explanatory. AMP connections are bi-directional, so try modifying the example so that the server asks the client for its current disk usage. The
twisted.protocols.amp
API docs will be useful here.When you're ready to build a real application read Twisted from Scratch, or The Evolution of Finger.
AMP has been implemented in most of the popular languages, but if you're looking for something more 'mainstream', protobuf is Google's thing. IMO it's overcomplicated and lacks some important features like error feedback.
你可以尝试一下twisted的“透视经纪人”。它有一些很好的功能,例如对称性(一旦建立连接,客户端和服务器之间没有真正的区别)。它负责序列化本身。如果您只想进行简单的单向状态更新推送,那么它可能不是最佳选择。
You could try twisted's “perspective broker“. It has a few nice features, such as symmetry (there is no real difference betweenclient and server, once a connection is established). It takes care of serialization itself. It might not be the best choice, if you only want a simple one-way status update push.
对于数据序列化,如果您使用 pickle 序列化自定义类实例,请确保服务器和客户端在全局命名空间中具有相同的类定义。
对于扭曲的参考,您可以查看其文档,尤其是“编写 TCP 服务器”和“编写 TCP 客户端”部分。
For data serializing, if you are using pickle to serialize custom class instances, do make sure that both the server and the client have the same class definition in the global namespace.
For twisted reference, you may take a look at its documentation, especially the 'Writing a TCP server' and 'Writing a TCP client' part.
如果服务器和客户端都是用 Python 编写的,最简单的方法就是从客户端向服务器发出 HTTP POST 请求,将数据序列化为 pickled Python 对象。 Pickles 将携带所有原始对象结构。
如果客户端是非 Pythonic 进程,那么 JSON 就是最佳选择 - 您只需将对象序列化为 JSON。 pickles 和 JSON 之间的区别在于 JSON 不能传输类,只能传输嵌套列表和字典等数据。
带有 JSON 负载的 HTTP POST - 这就是我要做的。
不要为 SOAP 烦恼。 WSDL 等只是给流程增加了额外的复杂性,非常不符合 Python 风格,而且 Python SOAP 库也不是那么健壮。
Twisted 文档不涵盖此类内容,因为这是一个通用的 Python 问题,而不是 Twisted 特有的问题。
If both server and client are written in Python the simplest way is just to do a HTTP POST request from the cliente to the server having data serialized to pickled Python objects. Pickles will carry all the orignal object structure.
If the client is non-pythonic process then JSON is the way to go - you just serialize objects to JSON. The difference between pickles and JSON is that JSON cannot transport classes, just data as nested lists and dicts.
HTTP POSTs with JSON payload - this is what I would do.
Do not bother with SOAP. WSDL etc. adds just extra complexity to the process, is very un-pythonic and Python SOAP libraries are not that robust.
Twisted documentation does not cover this kind of stuff, because this is a generic Python problem, not specific to Twisted.