2010-04-14 120 views
2

我的一個協議連接到一個服務器,並與我希望將其發送到其他協議的輸出。從一個協議發送數據到扭曲的另一個協議?

我需要從ClassB的訪問 '味精' 的方法在ClassA的,但我不斷收到: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() 

任何想法嗎? :-)

+0

可以粘貼實際的代碼? 理想情況下,一個最小的非工作示例。因此,可能有六個原因導致它無法正常工作。 – moshez 2010-04-14 02:25:17

+0

對不起 - http://pastebin.com/MQPhduSY – veb 2010-04-14 02:36:30

+1

你能發佈完整的錯誤信息嗎? – zoli2k 2010-04-14 11:42:05

回答

4

扭曲FAQ

如何使輸入一個連接 結果在輸出上的另一個?

這看起來就像是一個扭曲的 的問題,但實際上它是一個Python 問題。每個協議對象 代表一個連接;你可以用 來調用它的transport.write來寫一些 的數據給它。這些是常用的Python 對象;您可以將它們放入列表中, 字典或任何其他數據 結構適用於您的 應用程序。

舉一個簡單的例子,添加一個列表來 你的工廠,並在協議的 connectionMade和connectionLost,將其添加到 並從列表中刪除。 這裏的Python代碼:

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() 
相關問題