在 Twisted 中将数据从一种协议发送到另一种协议?

发布于 2024-08-28 23:47:16 字数 2388 浏览 4 评论 0原文

我的一个协议连接到服务器,我想将其输出发送到另一个协议。

我需要从 ClassB 访问 ClassA 中的“msg”方法,但我不断收到: exceptions.AttributeError: 'NoneType' object has no attribute 'write'

实际代码:

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import reactor

IRC_USERNAME = 'xxx'
IRC_CHANNEL = '#xxx'
T_USERNAME = 'xxx'
T_PASSWORD = md5.new('xxx').hexdigest()

class ircBot(irc.IRCClient):
    def _get_nickname(self):
        return self.factory.nickname

    nickname = property(_get_nickname)

    def signedOn(self):
        self.join(self.factory.channel)
        print "Signed on as %s." % (self.nickname,)

    def joined(self, channel):
        print "Joined %s." % (channel,)

    def privmsg(self, user, channel, msg):
        if not user:
                return

        who = "%s: " % (user.split('!', 1)[0], )
        print "%s %s" % (who, msg)

class ircBotFactory(protocol.ClientFactory):
    protocol = ircBot

    def __init__(self, channel, nickname=IRC_USERNAME):
        self.channel = channel
        self.nickname = nickname

    def clientConnectionLost(self, connector, reason):
        print "Lost connection (%s), reconnecting." % (reason,)
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "Could not connect: %s" % (reason,)

class SomeTClass(Protocol):
    def dataReceived(self, data):
        if data.startswith('SAY'):
                data = data.split(';', 1)
                # RAGE
                #return self.ircClient.msg(IRC_CHANNEL, 'test')

    def connectionMade(self):
        self.transport.write("mlogin %s %s\n" % (T_USERNAME, T_PASSWORD))

class tClientFactory(ClientFactory):
    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        return t()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason

if __name__ == "__main__":
    #chan = sys.argv[1]
    reactor.connectTCP('xxx', 6667, ircBotFactory(IRC_CHANNEL) )
    reactor.connectTCP('xxx', 20184, tClientFactory() )
    reactor.run()

请问有什么想法吗? :-)

One of my protocols is connected to a server, and with the output of that I'd like to send it to the other protocol.

I need to access the 'msg' method in ClassA from ClassB but I keep getting: exceptions.AttributeError: 'NoneType' object has no attribute 'write'

Actual code:

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import reactor

IRC_USERNAME = 'xxx'
IRC_CHANNEL = '#xxx'
T_USERNAME = 'xxx'
T_PASSWORD = md5.new('xxx').hexdigest()

class ircBot(irc.IRCClient):
    def _get_nickname(self):
        return self.factory.nickname

    nickname = property(_get_nickname)

    def signedOn(self):
        self.join(self.factory.channel)
        print "Signed on as %s." % (self.nickname,)

    def joined(self, channel):
        print "Joined %s." % (channel,)

    def privmsg(self, user, channel, msg):
        if not user:
                return

        who = "%s: " % (user.split('!', 1)[0], )
        print "%s %s" % (who, msg)

class ircBotFactory(protocol.ClientFactory):
    protocol = ircBot

    def __init__(self, channel, nickname=IRC_USERNAME):
        self.channel = channel
        self.nickname = nickname

    def clientConnectionLost(self, connector, reason):
        print "Lost connection (%s), reconnecting." % (reason,)
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "Could not connect: %s" % (reason,)

class SomeTClass(Protocol):
    def dataReceived(self, data):
        if data.startswith('SAY'):
                data = data.split(';', 1)
                # RAGE
                #return self.ircClient.msg(IRC_CHANNEL, 'test')

    def connectionMade(self):
        self.transport.write("mlogin %s %s\n" % (T_USERNAME, T_PASSWORD))

class tClientFactory(ClientFactory):
    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        return t()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason

if __name__ == "__main__":
    #chan = sys.argv[1]
    reactor.connectTCP('xxx', 6667, ircBotFactory(IRC_CHANNEL) )
    reactor.connectTCP('xxx', 20184, tClientFactory() )
    reactor.run()

Any ideas please? :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜雨飘雪 2024-09-04 23:47:16

Twisted 常见问题解答

如何在一个连接上进行输入
结果在另一个上输出?

这看起来像是一个扭曲的
问题,但实际上它是一个Python
问题。每个协议对象
代表一个连接;你可以
调用它的transport.write来写一些
数据给它。这些是常规的Python
物体;你可以将它们放入列表中,
字典,或任何其他数据
结构适合您
应用程序。

作为一个简单的示例,将列表添加到
你的工厂,以及你的协议中
连接建立和连接丢失,添加
并将其从该列表中删除。
Python 代码如下:

from twins.internet.protocol 导入协议、工厂
从twisted.internet导入反应器

MultiEcho 类(协议):
    def 连接建立(自身):
        self.factory.echoers.append(self)
    def dataReceived(自身,数据):
        对于 self.factory.echoers 中的 echoer:
            echoer.transport.write(数据)
    def 连接丢失(自身,原因):
        self.factory.echoers.remove(self)

MultiEchoFactory 类(工厂):
    协议 = MultiEcho
    def __init__(自身):
        self.echoers = []

reactor.listenTCP(4321, MultiEchoFactory())
反应堆.run()

Twisted FAQ:

How do I make input on one connection
result in output on another?

This seems like it's a Twisted
question, but actually it's a Python
question. Each Protocol object
represents one connection; you can
call its transport.write to write some
data to it. These are regular Python
objects; you can put them into lists,
dictionaries, or whatever other data
structure is appropriate to your
application.

As a simple example, add a list to
your factory, and in your protocol's
connectionMade and connectionLost, add
it to and remove it from that list.
Here's the Python code:

from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor

class MultiEcho(Protocol):
    def connectionMade(self):
        self.factory.echoers.append(self)
    def dataReceived(self, data):
        for echoer in self.factory.echoers:
            echoer.transport.write(data)
    def connectionLost(self, reason):
        self.factory.echoers.remove(self)

class MultiEchoFactory(Factory):
    protocol = MultiEcho
    def __init__(self):
        self.echoers = []

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