2010-04-14 5 views
2

내 프로토콜 중 하나가 서버에 연결되어 있으며 그 출력으로 다른 프로토콜로 보내고 싶습니다.하나의 프로토콜에서 다른 프로토콜로 데이터를 전송하는 중?

내가 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() 

어떤 아이디어하세요? :-)

+0

당신이 실제 코드를 붙여 넣을 수 있습니다 : 다음은 파이썬 코드입니까? 최소한의 비 작동 예제가 이상적입니다. 그렇다고해서 그것이 작동하지 않는 이유가 여섯 개있을 수 있습니다. – moshez

+0

죄송합니다 - http://pastebin.com/MQPhduSY – veb

+1

전체 오류 메시지도 게시 할 수 있습니까? – zoli2k

답변

4

트위스트 FAQ :

어떻게 서로 출력에 하나 개의 연결 결과에 입력을 어떻게해야합니까?

그것은 트위스트 질문처럼이 보이지만, 실제로는 파이썬 질문입니다. 각 프로토콜 객체 은 하나의 연결을 나타냅니다. 당신은 그것의 transport.write라고 부르면 데이터를 보낼 수 있습니다. 이것들은 일반적인 파이썬 객체입니다. 사전 또는 기타 데이터 구조를 목록에 넣을 수 있습니다. 응용 프로그램에 적합합니다.

간단한 예를 들어, 당신의 공장에 목록을 추가하고 프로토콜의 connectionMade 및 connectionLost에,에 을 추가하고 그 목록에서 제거합니다.

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() 
관련 문제