gpt4 book ai didi

python - 在 Twisted 中将数据从一个协议(protocol)发送到另一个协议(protocol)?

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:44 24 4
gpt4 key购买 nike

我的一个协议(protocol)连接到一个服务器,并且我想将它的输出发送到另一个协议(protocol)。

我需要从 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()

有什么想法吗? :-)

最佳答案

扭曲 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()

关于python - 在 Twisted 中将数据从一个协议(protocol)发送到另一个协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2634494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com