2012-12-09 4 views
0

트위스트 튜토리얼 문서에서 수정 된 간단한 "견적"서버 및 클라이언트를 작성하려고합니다. 나는 오늘의 "견적"이 클라이언트에서 인쇄되어 통신을 증명하기를 원합니다. 그러나, 내가 말할 수있는 것은 클라이언트가 연결되지 않는다는 것입니다. 여기 내 코드가있다.파이썬 트위스트 단순 서버 클라이언트

서버

from twisted.python import log 
from twisted.internet.protocol import Protocol 
from twisted.internet.protocol import Factory 
from twisted.internet.endpoints import TCP4ServerEndpoint 
from twisted.internet import reactor 

class QOTD(Protocol): 
    def connectionMade(self): 
     self.transport.write("An apple a day keeps the doctor away\r\n") 
     self.transport.loseConnection() 

class QOTDFactory(Factory): 
    def buildProtocol(self, addr): 
     return QOTD() 

# 8007 is the port you want to run under. Choose something >1024 
endpoint = TCP4ServerEndpoint(reactor, 8007) 
endpoint.listen(QOTDFactory()) 
reactor.run() 

클라이언트

import sys 
from twisted.python import log 
from twisted.internet import reactor 
from twisted.internet.protocol import Factory, Protocol 
from twisted.internet.endpoints import TCP4ClientEndpoint 

class SimpleClient(Protocol): 
    def connectionMade(self): 
     log.msg("connection made") 
     #self.transport.loseConnection() 

    def lineReceived(self, line): 
     print "receive:", line 

class SimpleClientFactory(Factory): 
    def buildProtocol(self, addr): 
     return SimpleClient() 

def startlog(): 
    log.startLogging(sys.stdout) 

point = TCP4ClientEndpoint(reactor, "localhost", 8007) 
d = point.connect(SimpleClientFactory) 
reactor.callLater(0.1, startlog) 
reactor.run() 

답변

2
  • SimpleClientFactory의 패스 인스턴스가 아닌 클래스 자체 twisted.protocol.basic.LineReceiver에서 대신 프로토콜의 point.connect()
  • 서브 클래스 SimpleClient 경우에 당신은 lineReceived
  • 을 사용합니다. endpoint.listenpoint.connect의 결과에 addErrback을 호출하여 오류를 처리하십시오.
+0

작동시킨 LineReceiver에서 서브 클래 싱 중이 었습니다. 감사! –

관련 문제