为什么“商业客户工厂”这个类没有被理解为pycharm中的定义?

2024-09-28 22:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我学习网络编程与o'REilys扭曲网络编程基本指南(使用pyde)

我有两个问题: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()

Tags: self网络youconnectordef编程counterquotes
1条回答
网友
1楼 · 发布于 2024-09-28 22:20:01

你的压痕不对。这有点难看,因为代码跨越了一个分页符。你想要的是:

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

相关问题 更多 >