簡體   English   中英

downloadPage回調函數問題

[英]Issue with downloadPage callback function

我寫了以下python腳本:

from twisted.internet import defer             
from twisted.web.client import getPage, downloadPage, reactor
import tempfile

def success(results):
  print 'success'   

def error(results):
  print 'error', results
  reactor.stop()

tmpfilename = tempfile.mkstemp()
downloadPage('http://www.google.com', tmpfilename).addCallback(success).addErrback(error)

reactor.run()

我收到以下錯誤:

Unhandled Error
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/twisted/python/log.py", line 88, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/python/log.py", line 73, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/selectreactor.py", line 151, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 215, in doRead
    return self._dataReceived(data)
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 221, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/usr/local/lib/python2.7/site-packages/twisted/protocols/basic.py", line 578, in dataReceived
    why = self.rawDataReceived(data)
  File "/usr/local/lib/python2.7/site-packages/twisted/web/http.py", line 518, in rawDataReceived
    self.handleResponsePart(data)
  File "/usr/local/lib/python2.7/site-packages/twisted/web/client.py", line 249, in handleResponsePart
    self.factory.pagePart(data)
  File "/usr/local/lib/python2.7/site-packages/twisted/web/client.py", line 504, in pagePart
    self.file.write(data)
exceptions.AttributeError: 'tuple' object has no attribute 'write'
Unhandled Error
Traceback (most recent call last):
  File "poc.py", line 16, in <module>
    reactor.run()
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1192, in run
    self.mainLoop()
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1204, in mainLoop
    self.doIteration(t)
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/selectreactor.py", line 145, in doSelect
    _logrun(selectable, _drdw, selectable, method)
--- <exception caught here> ---
  File "/usr/local/lib/python2.7/site-packages/twisted/python/log.py", line 88, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/python/log.py", line 73, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/selectreactor.py", line 156, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, method=="doRead")
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 263, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 485, in connectionLost
    self._commonConnection.connectionLost(self, reason)
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 299, in connectionLost
    protocol.connectionLost(reason)
  File "/usr/local/lib/python2.7/site-packages/twisted/web/client.py", line 198, in connectionLost
    http.HTTPClient.connectionLost(self, reason)
  File "/usr/local/lib/python2.7/site-packages/twisted/web/http.py", line 472, in connectionLost
    self.handleResponseEnd()
  File "/usr/local/lib/python2.7/site-packages/twisted/web/client.py", line 258, in handleResponseEnd
    self.factory.pageEnd()
  File "/usr/local/lib/python2.7/site-packages/twisted/web/client.py", line 531, in pageEnd
    self.file.close()
exceptions.AttributeError: 'tuple' object has no attribute 'close'

如果我將url更改為無效的東西,它將拋出正確的錯誤回調函數,因此它似乎與成功回調有關但是我無法理解為什么。

在這之后:

tmpfilename = tempfile.mkstemp()

tmpfilename的值是一個元組(請參閱docs ),但twisted需要一個文件名或類文件對象。

所以你可以這樣做:

tmpfile = tempfile.mkstemp()
tmpfilename = tmpfile[1]
downloadPage('http://www.google.com', tmpfilename).addCallback(success).addErrback(error)

哪個有效。

但是,如果你不需要該文件持續存在,我建議像:

tmpfile = tempfile.TemporaryFile()
downloadPage('http://www.google.com', tmpfile).addCallback(success).addErrback(error)

它使用TemporaryFile()構造函數,允許您訪問下載的數據,但是一旦進程關閉,該文件(無論出於何種意圖和目的)都不會再被看到。

您可以使用上下文管理器進一步改進這一點 - 例如:

with tempfile.TemporaryFile() as tmpfile:
    downloadPage('http://www.google.com', tmpfile).addCallback(success).addErrback(error)

    # do other stuff with tmpfile

# code that no longer depends on the existence of tmpfile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM