【问题标题】:Frequent ChunkedEncodingError with Google App Engine / RequestsGoogle App Engine / 请求频繁出现 ChunkedEncodingError
【发布时间】:2016-05-06 09:54:24
【问题描述】:

在使用 Requests (Python) 和 Google App Engine 请求服务器时,我经常收到 ChunkedEncodingError。

我查看了来自 IncompleteRead using httplib 的答案,但问题是我不相信我的问题与查询服务器有关:我经常在使用的各种端点(包括对讲机和 FullContact)上遇到此错误。

如果问题总是从同一台服务器(例如 FullContact)引发,我会怀疑该问题与一项服务的服务器有关,但事实并非如此。我在其他不相关的请求中也遇到了这个问题。

所以我怀疑问题出在我的代码或 Google 上。但是从我的代码“角度”来看,我不知道会出现什么问题。这是一个sn-p:

result = requests.post(
    "https://api.intercom.io/companies",
    json={'some': 'data', 'that': 'are', 'sent': 'ok'},
    headers={'Accept': 'application/json'},
    auth=("app_id", "app_key",)
)

如您所见,请求非常标准,没有什么花哨的。它也因以下简单的事情而失败:

r = requests.get(url, params=params, timeout=3)

有人在使用 Google App Engine 时遇到过这些问题吗?有什么办法可以避免这种情况吗?

【问题讨论】:

  • 您使用的是什么版本的请求?
  • 我使用的是 requests 2.9.1,为什么? (和 Python 2.7)
  • 在 2.10.0 之前,请求不完全支持 GAE。您应该升级并查看问题是否仍然存在。
  • 好的,我切换到最新版本并推送代码。到目前为止,一切都很好。我会等几天再回来找你。您已经可以发表您的评论作为答案,我已经投票了。

标签: python google-app-engine python-requests


【解决方案1】:

有一个(似乎)适用于 GAE 的补丁。 问题出在 requests 的 iter_content 函数中,该函数使用了后续的 urllib3 库。

问题在于 Google 为自己的实现重写了此库,但进行了一些更改,导致 Requests 级别的 ChunkedEncodingError。

我试过this patch,到目前为止,一切都很好。具体来说,您必须在 requests/models.py 文件中替换以下行:

for chunk in self.raw.stream(chunk_size, decode_content=True):
    yield chunk

作者:

if isinstance(self.raw._original_response._method, int):
    while True:
        chunk = self.raw.read(chunk_size, decode_content=True)
        if not chunk:
            break
        yield chunk
else:
    for chunk in self.raw.stream(chunk_size, decode_content=True):
        yield chunk

问题就会停止。

我 submitted an issue 会在 Requests 存储库中讨论它,我们将看看它会如何发展。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多