python - downloadPage 回调函数的问题

标签 python asynchronous twisted

我编写了以下 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

关于python - downloadPage 回调函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23337566/

相关文章:

Python 错误 : OSError: [Errno 22] Invalid argument

python - Quichesort 的好处

java - 将RSS feed写入MySQL

javascript - html注入(inject)中的jQuery脚本执行顺序

python - 如何在 Twisted 中分离协议(protocol)的两个不同阶段?

python - 我可以使用 Docker 使用 pyinstaller 创建 exe 吗

javascript - 使用带有 Fetch API 响应的 promise 仍然会使我的数据返回为未定义

javascript - Node JS - 从异步函数获取数据

python - 在 Mac OS X 上安装 Python Twisted

python - 使用 IPC、Twisted 或 ZeroMQ 的架构方法?