Twisted Python getPage

时间:2010-04-20 00:26:58

标签: python twisted

我试图获得支持,但我完全糊涂了。

这是我的代码:


from twisted.internet import reactor
from twisted.web.client import getPage
from twisted.web.error import Error
from twisted.internet.defer import DeferredList
from sys import argv

class GrabPage:
 def __init__(self, page):
  self.page = page

 def start(self, *args):
  if args == ():
   # We apparently don't need authentication for this
   d1 = getPage(self.page)
  else:
   if len(args) == 2:
    # We have our login information
    d1 = getPage(self.page, headers={"Authorization": " ".join(args)})
   else:
    raise Exception('Missing parameters')

  d1.addCallback(self.pageCallback)
  dl = DeferredList([d1])
  d1.addErrback(self.errorHandler)
  dl.addCallback(self.listCallback)

 def errorHandler(self,result):
  # Bad thingy!
  pass

 def pageCallback(self, result):
  return result

 def listCallback(self, result):
  print result

a = GrabPage('http://www.google.com')
data = a.start() # Not the HTML

我希望在调用start()时获取给予pageCallback的HTML。这对我来说是一个皮塔饼。泰!对不起我的糟糕编码。

1 个答案:

答案 0 :(得分:14)

你错过了Twisted如何运作的基础知识。这一切都围绕reactor,你甚至从未运行。想想反应堆是这样的:

Reactor Loop http://krondo.com/blog/wp-content/uploads/2009/07/reactor-1.png

在你启动反应堆之前,通过设置延迟,你正在做的就是链接它们,没有任何可以触发的事件。

我建议您通过Twisted Intro阅读Dave Peticolas。它很快,它确实为您提供了Twisted文档所没有的所有缺失信息。

无论如何,这是getPage最基本的用法示例:

from twisted.web.client import getPage
from twisted.internet import reactor

url = 'http://aol.com'

def print_and_stop(output):
    print output
    if reactor.running:
       reactor.stop()

if __name__ == '__main__':
    print 'fetching', url
    d = getPage(url)
    d.addCallback(print_and_stop)
    reactor.run()

由于getPage返回延迟,我将回调print_and_stop添加到延迟链。之后,我开始reactor。反应器触发getPage,然后触发print_and_stop,从aol.com打印数据,然后停止反应堆。

编辑以显示OP代码的工作示例:

class GrabPage:
    def __init__(self, page):
        self.page = page
        ########### I added this:
        self.data = None

    def start(self, *args):
        if args == ():
            # We apparently don't need authentication for this
            d1 = getPage(self.page)
        else:
            if len(args) == 2:
                # We have our login information
                d1 = getPage(self.page, headers={"Authorization": " ".join(args)})
            else:
                raise Exception('Missing parameters')

        d1.addCallback(self.pageCallback)
        dl = DeferredList([d1])
        d1.addErrback(self.errorHandler)
        dl.addCallback(self.listCallback)

    def errorHandler(self,result):
        # Bad thingy!
        pass

    def pageCallback(self, result):
        ########### I added this, to hold the data:
        self.data = result
        return result

    def listCallback(self, result):
        print result
        # Added for effect:
        if reactor.running:
            reactor.stop()

a = GrabPage('http://google.com')
########### Just call it without assigning to data
#data = a.start() # Not the HTML
a.start()

########### I added this:
if not reactor.running:
    reactor.run()

########### Reference the data attribute from the class
data = a.data
print '------REACTOR STOPPED------'
print
########### First 100 characters of a.data:
print '------a.data[:100]------'
print data[:100]