Twisted - 如何创建多协议进程并在协议之间发送数据
我正在尝试编写一个程序,该程序将在某个端口(例如 tcp 6666)上侦听数据(简单的文本消息),然后将它们传递给一个或多个不同的协议 - irc、xmpp 等。我尝试了很多方法并挖掘了互联网,但我找不到完成此类任务的简单且可行的解决方案。
我目前正在处理的代码在这里: http://pastebin.com/ri7caXih
我想知道如何从像这样的对象:
ircf = ircFactory('asdfasdf', '#asdf666')
访问自我协议方法,因为:
self.protocol.dupa1(msg)
返回有关自我未传递到活动协议对象的错误。或者也许还有其他更好、更简单、更彻底的方法来创建具有多个协议的单个反应器,并在消息到达其中任何一个时触发操作,然后将该消息传递给其他协议进行处理/处理/发送?
任何帮助将不胜感激!
Im trying to write a program that would be listening for data (simple text messages) on some port (say tcp 6666) and then pass them to one or more different protocols - irc, xmpp and so on. I've tried many approaches and digged the Internet, but I cant find easy and working solution for such task.
The code I am currently fighting with is here: http://pastebin.com/ri7caXih
I would like to know how to from object like:
ircf = ircFactory('asdfasdf', '#asdf666')
get access to self protocol methods, because this:
self.protocol.dupa1(msg)
returns error about self not being passed to active protocol object. Or maybe there is other, better, easier and more kosher way to create single reactor with multiple protocols and have actions triggeres when a message arrives on any of them, and then pass that message to other protocols for handling/processing/sending?
Any help will be highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是从端口 9001 的多个连接读取数据并写入端口 9000 上的连接的示例代码。您需要多个“PutLine”实现,其中一个用于 XMPP、IRC、MSN 等。
我使用全局来存储输出连接PutLine 但您可能希望创建一个更复杂的 Factory 对象来处理此问题。
测试:
从任意数量的 nc 9001(或 netcat 9001)输入的数据将出现在 nc -l 9000 上。
Here is sample code to read from multiple connections to port 9001 and write out to a connection on port 9000. You would need multiple "PutLine" implementations, one for XMPP, IRC, MSN, etc.
I used a global to store the output connection PutLine but you would want to create a more complex Factory object that would handle this instead.
Testing:
Data entered form any number of nc 9001 (or netcat 9001) will appear on nc -l 9000.
这在常见问题解答中得到了解答。
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother
This is answered in the FAQ.
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother
请参阅
doc/core/examples/chatserver.py< /代码>
。他们在
Protocol
的connectionMade
和connectionLost
方法中添加了挂钩,以维护已连接客户端的列表,然后迭代所有客户端当消息到达时进行传递。See
doc/core/examples/chatserver.py
. There they've added hooks to theProtocol
'sconnectionMade
andconnectionLost
methods to maintain a list of connected clients, and then it iterates through all of them when a message arrives to pass on.