用于允许断开连接的仅发送协议的扭曲客户端
我决定在twisted 的帮助下涉足异步Python 的世界。我已经实现了文档中的一些示例,但是我很难找到我正在尝试编写的非常简单的客户端示例。
简而言之,我想要一个客户端与服务器建立 TCP 连接,然后将简单的“\n”终止字符串消息从队列对象发送到服务器。服务器不会响应任何消息,因此我的客户端是完全单向的。我/认为/我想要的是这个示例的某种组合 和twisted.internet.protocols.basic.LineReceiver 便利协议。感觉这应该是扭曲中可以做的最简单的事情,但我在网上看到的文档或示例似乎都不太合适。
I've decided to dip my toe into the world of asynchronous python with the help of twisted. I've implemented some of the examples from the documentation, but I'm having a difficult time finding an example of the, very simple, client I'm trying to write.
In short I'd like a client which establishes a tcp connection with a server and then sends simple "\n" terminated string messages off of a queue object to the server. The server doesn't ever respond with any messages so my client is fully unidirectional. I /think/ that what I want is some combination of this example and the twisted.internet.protocols.basic.LineReceiver convenience protocol. This feels like it should be just about the simplest thing one could do in twisted, but none of the documentation or examples I've seen online seem to fit quite right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所做的没有使用队列,但我正在说明一旦建立连接就发送一行的代码。有很多印刷材料可以帮助您了解正在发生的事情。
常见的导入内容:
您创建一个从线路接收器派生的协议,设置分隔符。
在这种情况下,一旦建立连接,我只需编写一个字符串“www”。
关键是查看twisted.internet.interface.py 上的协议接口并了解协议的各种方法以及它们的作用和调用时间。
最后,一个协议工厂将为每个连接创建一个协议实例。
查看方法:buildProtcol。
现在您使用连接器来建立连接:
我运行了它并将其连接到一个服务器,该服务器仅打印它收到的内容,因此不发送回确认。这是输出:
上面的例子如果不是容错的。要重新连接,当连接丢失时,您可以从现有的扭曲类 - ReconnectingClientFactory 派生协议工厂。
Twisted 几乎拥有您需要的所有工具:)
我建议您阅读:http://krondo.com/?page_id=1327
[编辑:根据下面的评论]
What I have done is not used a Queue but I am illustrating the code that sends a line, once a connection is made. There are bunch of print stuff that will help you understand on what is going on.
Usual import stuff:
You create a protocol derived from line receiver, set the delimiter.
In this case, I simply write a string "www" once the connection is made.
The key thing is to look at protocol interface at twisted.internet.interface.py and understand the various methods of protocol and what they do and when they are called.
Finally, A protocol factory that will create a protocol instance for every connection.
Look at method : buildProtcol.
Now you use a connector to make a connection:
I ran this and connected it to an server that simply prints what it receives and hence send no ack back. Here is the output:
The above example if not fault tolerant. To reconnect , when a connection is lost, you can derive your protocol factory from an existing twisted class - ReconnectingClientFactory.
Twisted has almost all the tools that you would need :)
I suggest that you read : http://krondo.com/?page_id=1327
[Edited: As per comment below]