python - 扭曲给出twisted.web.client.PartialDownloadError : 200 OK

标签 python twisted

我有以下代码片段,对原始 docs 稍作修改。当 url 设置为 http://google.com 时,代码可以正常工作。 。但是当它更改为 http://www.google.com 时,它会崩溃。 。崩溃时的错误是Failure:twisted.web.client.PartialDownloadError:200 OK。回溯位于代码片段下方。

最初我认为代码可能由于没有正确处理 SSL 而崩溃。但是,看看标题,这似乎不是问题。这是我第一次与 Twisted 合作;我不知道还有什么可能导致这个问题。

代码

from sys import argv
from pprint import pformat
from twisted.internet.task import react
from twisted.web.client import Agent, BrowserLikeRedirectAgent, readBody
from twisted.web.http_headers import Headers
from twisted.internet import reactor
from twisted.internet.ssl import ClientContextFactory

responses = []

class WebClientContextFactory(ClientContextFactory):
    def getContext(self, hostname, port):
        return ClientContextFactory.getContext(self)

def cbBody(r):
    print 'Response body:'
    print r
    responses.append(r)

def cbRequest(response):
    print 'Response version:', response.version
    print 'Response code:', response.code
    print 'Response phrase:', response.phrase
    print 'Response headers:'
    print pformat(list(response.headers.getAllRawHeaders()))
    d = readBody(response)
    d.addCallback(cbBody)
    return d

def main(reactor):
    contextFactory = WebClientContextFactory()
    agent = BrowserLikeRedirectAgent(Agent(reactor, contextFactory))
    url=b"http://google.com/"
    agent = Agent(reactor, contextFactory)
    d = agent.request(
        'GET', url,
        Headers({'User-Agent': ['Twisted Web Client Example']}),
        None)
    d.addCallback(cbRequest)
    return d

react(main)

回溯

In [1]: %tb
---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
/usr/local/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, glob, loc, compiler)
    218             else:
    219                 scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
--> 220                 exec(compiler(scripttext, filename, 'exec'), glob, loc)
    221
    222

/project/demo.py in <module>()
     42     return d
     43
---> 44 react(main)

/usr/local/lib/python2.7/site-packages/twisted/internet/task.pyc in react(main, argv, _reactor)
    902     finished.addBoth(cbFinish)
    903     _reactor.run()
--> 904     sys.exit(codes[0])
    905
    906

SystemExit: 1

最佳答案

对不同 URL 的请求会产生不同的响应,这并不奇怪。 URL 标识不同的资源。您可能应该期望在请求不同的资源时得到不同的响应。

当您请求 http://www.google.com/ 时,您收到 PartialDownloadError 的原因是 Google 发送的响应中没有包含 Content -Length 也不是其中的 Transfer-Encoding: chunked 。这意味着客户端知道何时收到响应的唯一方法是 TCP 连接何时关闭。不幸的是,TCP 连接可能会因其他原因关闭 - 因此是否完全收到响应是不明确的。

Google 似乎以这种方式构建响应,以响应 Agent 如何发出请求的特定细节。 Google 使用 Transfer-Encoding: chunked 响应其他代理发出的请求。

解决此问题的一个选择是决定您不关心响应是否在您不知情的情况下被截断。在这种情况下,将 errback 添加到处理 PartialDownloadErrorreadBody Deferred 中。该异常有一个 response 属性,为您提供在 TCP 连接关闭之前读取的数据。获取该数据并将其返回,现在您已将可能失败的案例转换为谁在乎假装成功的案例。

另一种选择是尝试修改请求的详细信息,直到您说服 Google 为您提供一个 Transfer-Encoding: chunked (或至少一个 Content-Length )。当然,一旦您遇到另一台服务器不愿意为您提供其中一个或另一个,此解决方案就会失效。

关于python - 扭曲给出twisted.web.client.PartialDownloadError : 200 OK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29423986/

相关文章:

python - python中的Imageio : compressing gif

Python 元组列表

python - 从父级获取变量

python - 在Python中将递归转换为迭代

python - 扭曲插件错误

python - 检测协程是否需要异步循环

python - Pandas 数据框如何用具有附加属性的行替换行

Python 扭曲 : where to start

python - 了解异步IO : vs asynchronous programming

python - 以 root 身份运行 twistd,找不到模块