代码更改 - 使用python2更改为python3

时间:2018-01-09 23:10:56

标签: python networking twisted

这是来自O' Reily Twisted Network编程必备手册的示例代码。行"h = HeadlineRetriever()导致问题。错误日志显示' HeadlineRetriever'没有定义。这是由于py3的变化吗?我该如何解决这个问题?

尝试:取消行h = HeadlineRetrieveer(),但行d.addCallbacks(printData, printError)无法识别参数。

from twisted.internet import reactor, defer

    class HeadlineRetriever(object):
        def processHeadline(self, headline):
            if len(headline) > 50:
                self.d.errback(
                    b"The headline ''%s'' is too long!" % (headline,))
            else:
                self.d.callback(headline)

        def _toHTML(self, result):
            return "<h1>%s</h1>" % (result,)

        def getHeadline(self, input):
            self.d = defer.Deferred()
            reactor.callLater(1, self.processHeadline, input)
            self.d.addCallback(self._toHTML)
            return self.d

        def printData(result):
            print(result)
            reactor.stop()

        def printError(failure):
            print(failure)
            reactor.stop()

        h = HeadlineRetriever()
        d = h.getHeadline("Breaking News: Twisted Takes Us to the Moon!")
        d.addCallbacks(printData, printError)

        reactor.run()

错误日志:回溯(最近一次呼叫最后一次):

  File "C:/Users/jessica/Twisted/3.4asynchronousHeadlineRetriever.py", line 3, in <module>
    class HeadlineRetriever(object):
  File "C:/Users/jessica/Twisted/3.4asynchronousHeadlineRetriever.py", line 28, in HeadlineRetriever
    h = HeadlineRetriever()
NameError: name 'HeadlineRetriever' is not defined

3 个答案:

答案 0 :(得分:0)

我认为在你定义h和类本身时会出现一个识别错误

from twisted.internet import reactor, defer

class HeadlineRetriever(object):
    def processHeadline(self, headline):
        if len(headline) > 50:
            self.d.errback(
                b"The headline ''%s'' is too long!" % (headline,))
        else:
            self.d.callback(headline)

    def _toHTML(self, result):
        return "<h1>%s</h1>" % (result,)

    def getHeadline(self, input):
        self.d = defer.Deferred()
        reactor.callLater(1, self.processHeadline, input)
        self.d.addCallback(self._toHTML)
        return self.d

    def printData(result):
        print(result)
        reactor.stop()

    def printError(failure):
        print(failure)
        reactor.stop()

h = HeadlineRetriever()
d = h.getHeadline("Breaking News: Twisted Takes Us to the Moon!")
d.addCallbacks(printData, printError)

reactor.run()

这应该有效

答案 1 :(得分:0)

你好吗?

也许你错过了__init__方法,同样,保持你的代码在左边距缩进,如果不是,可能会导致你的代码变得有点小错误。

尝试添加:

 def __init__(self):
            pass

看看它是否有效,然后更新我。

干杯!

答案 2 :(得分:0)

    from twisted.internet import reactor, defer

        class HeadlineRetriever(object):
            def processHeadline(self, headline):
                if len(headline) > 50:
                    self.d.errback(
                        b"The headline ''%s'' is too long!" % (headline,))
                else:
                    self.d.callback(headline)

            def _toHTML(self, result):
                return "<h1>%s</h1>" % (result,)

            def getHeadline(self, input):
                self.d = defer.Deferred()
                reactor.callLater(1, self.processHeadline, input)
                self.d.addCallback(self._toHTML)
                return self.d

            def printData(self, result):
                print(result)
                reactor.stop()

            def printError(self, failure):
                print(failure)
                reactor.stop()

  h = HeadlineRetriever()
  d = h.getHeadline("Breaking News: Twisted Takes Us to the Moon!")
  d.addCallbacks(h.printData, h.printError)

  reactor.run()

printError和print data函数需要self作为第一个参数和调用。 d.adCallbacks(printData, printError)需要d.addcallbacks(h.printData, h.printError)