Twisted - 如何创建多协议进程并在协议之间发送数据

发布于 2024-08-26 22:00:01 字数 504 浏览 8 评论 0原文

我正在尝试编写一个程序,该程序将在某个端口(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洛阳烟雨空心柳 2024-09-02 22:00:01

下面是从端口 9001 的多个连接读取数据并写入端口 9000 上的连接的示例代码。您需要多个“PutLine”实现,其中一个用于 XMPP、IRC、MSN 等。

我使用全局来存储输出连接PutLine 但您可能希望创建一个更复杂的 Factory 对象来处理此问题。

#!/usr/bin/env python

from twisted.internet.protocol import Protocol, Factory
from twisted.internet.endpoints import clientFromString, serverFromString
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

queue = []
putter = None

class GetLine(LineReceiver):
    delimiter = '\n'

    def lineReceived(self, line):
        queue.append(line)
        putter.have_data()
        self.sendLine(line)

class PutLine(LineReceiver):
    def __init__(self):
        global putter
        putter = self
        print 'putline init called %s' % str(self)

    def have_data(self):
        line = queue.pop()
        self.sendLine(line)


def main():
    f = Factory()
    f.protocol = PutLine
    endpoint = clientFromString(reactor, "tcp:host=localhost:port=9000")
    endpoint.connect(f)
    f = Factory()
    f.protocol = GetLine
    endpoint2 = serverFromString(reactor, "tcp:port=9001")
    endpoint2.listen(f)
    reactor.run()

if __name__ == '__main__':
    main()

测试:

nc -l  9000
python test.py
nc 9001

从任意数量的 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.

#!/usr/bin/env python

from twisted.internet.protocol import Protocol, Factory
from twisted.internet.endpoints import clientFromString, serverFromString
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

queue = []
putter = None

class GetLine(LineReceiver):
    delimiter = '\n'

    def lineReceived(self, line):
        queue.append(line)
        putter.have_data()
        self.sendLine(line)

class PutLine(LineReceiver):
    def __init__(self):
        global putter
        putter = self
        print 'putline init called %s' % str(self)

    def have_data(self):
        line = queue.pop()
        self.sendLine(line)


def main():
    f = Factory()
    f.protocol = PutLine
    endpoint = clientFromString(reactor, "tcp:host=localhost:port=9000")
    endpoint.connect(f)
    f = Factory()
    f.protocol = GetLine
    endpoint2 = serverFromString(reactor, "tcp:port=9001")
    endpoint2.listen(f)
    reactor.run()

if __name__ == '__main__':
    main()

Testing:

nc -l  9000
python test.py
nc 9001

Data entered form any number of nc 9001 (or netcat 9001) will appear on nc -l 9000.

乞讨 2024-09-02 22:00:01

请参阅doc/core/examples/chatserver.py< /代码>。他们在 ProtocolconnectionMadeconnectionLost 方法中添加了挂钩,以维护已连接客户端的列表,然后迭代所有客户端当消息到达时进行传递。

See doc/core/examples/chatserver.py. There they've added hooks to the Protocol's connectionMade and connectionLost methods to maintain a list of connected clients, and then it iterates through all of them when a message arrives to pass on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文