2017-12-31 158 views
0

나는 o'Rilys Twisted Network Programming 필수 가이드로 네트워크 프로그래밍을 배우고 있습니다. (pycharm IDE를 사용하여)클래스 "QuoteClientFactory"가 pycharm에서 정의 된 것으로 이해되지 않는 이유는 무엇입니까?

나는 maybechopopactact()가 pycharm에서 인식되지 않고 QuoteClientFactory가 정의 된 클래스로 보이지 않는 두 가지 문제가 있습니다.

어떻게 해결할 수 있습니까?

class QuoteClientFactory(protocol.ClientFactory): 
    def __init__(self, quote): 
     self.quote = quote 

    def buildProtocol(self, addr): 
     return QuoteProtocol(self) 

    def clientConnectionFailed(self, connector, reason): 
     print("connecton failed:"), reason.getErrorMessage() 
     **maybeStopReactor()** 

    def clientConnectionLost(self, connector, reason): 
     print("connection lost"), reason.getErrorMessage() 
     maybeStopReactor() 

    def maybeStopReactor(self): 
     global quote_counter 
     quote_counter -=1 
     if not quote_counter: 
      reactor.stop() 

    quotes = [ 
     "you snooze you lose", 
     "The early bird gets the worm", 
     "carpe diem" 
    ] 

    quote_counter = len(quotes) 

    for quote in quotes: 
     **reactor.connectTCP('localhost', 6942, QuoteClientFactory(quote))** 
    reactor.run() 
+1

정의가 끝나기 전에 클래스를 참조 할 수 없습니다. 아직 정의되지 않았습니다. 메서드 본문은 클래스 정의 중에 실행되지 않으므로 클래스는 자체 메서드에서 사용할 수 있습니다. 어쩌면 당신은'quotes = ... '에서 시작하는 모든 것을 취소하고 싶습니다. – schwobaseggl

답변

1

들여 쓰기가 잘못되었습니다. 코드가 페이지 나누기에 걸쳐 있기 때문에보기가 약간 어렵습니다. 원하는 내용은 다음과 같습니다.

class QuoteClientFactory(protocol.ClientFactory): 
    def __init__(self, quote): 
     self.quote = quote 

    def buildProtocol(self, addr): 
     return QuoteProtocol(self) 

    def clientConnectionFailed(self, connector, reason): 
     print("connecton failed:"), reason.getErrorMessage() 
     maybeStopReactor() 

    def clientConnectionLost(self, connector, reason): 
     print("connection lost"), reason.getErrorMessage() 
     maybeStopReactor() 

def maybeStopReactor(): 
    global quote_counter 
    quote_counter -=1 
    if not quote_counter: 
     reactor.stop() 

quotes = [ 
    "you snooze you lose", 
    "The early bird gets the worm", 
    "carpe diem" 
] 

quote_counter = len(quotes) 

for quote in quotes: 
    reactor.connectTCP('localhost', 6942, QuoteClientFactory(quote)) 
reactor.run() 
+0

트위스트 북에서 샘플 출력은 "받은 인용문 : 초기 새는 웜을 얻습니다."하지만 내가 얻는 것은 단지 인용문입니다. 제안 사항이 있습니까? 이 문제를 해결하는 방법은? 나는 무엇을 찾고 있는지 확실하지 않습니까? –

+0

어떤 버전의 파이썬을 사용하고 있습니까? – Batman

+0

파이썬 3.6.2를 사용하고 있으며 pycharm을 IDE 용으로 사용하고 있습니다. 다른 IDE를 시도해야합니까? –