在 Twisted 中将数据从一种协议发送到另一种协议?
我的一个协议连接到服务器,我想将其输出发送到另一个协议。
我需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Twisted 常见问题解答:
Twisted FAQ: