【发布时间】:2017-12-31 17:24:33
【问题描述】:
我正在使用 o'REilys Twisted 网络编程必备指南学习网络编程。 (使用 pycharm IDE)
我有两个问题,pycharm 中没有识别函数 maybeStopReactor() 并且没有将 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()
【问题讨论】:
-
你不能在定义结束之前引用一个类,因为它还没有定义。请注意,在类定义期间不会执行方法体,因此可以在其自己的方法中使用该类。也许你想取消从
quotes = ...开始的所有内容
标签: python networking pycharm twisted