【问题标题】:Python requests: missing content-length responsePython 请求:缺少内容长度响应
【发布时间】:2020-05-24 19:14:30
【问题描述】:

Python 请求不会返回我可以通过 curl 获得的内容长度标头。

def get_headers(url):
    response = requests.head(url)
    return response.headers

回复:

{'Server': 'nginx/1.17.9', 'Date': 'Sun, 24 May 2020 19:09:02 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'Access-Control-Allow-Origin': '*', 'Content-Disposition': 'inline; filename="introduction - Sia API Documentation.html"', 'Skynet-File-Metadata': '{"filename":"introduction - Sia API Documentation.html"}', 'Access-Control-Expose-Headers': 'skynet-file-metadata', 'Content-Encoding': 'gzip'}

我也尝试了 requests.get,但也没有工作: response = requests.get(url, stream=True)

【问题讨论】:

    标签: python python-3.x get python-requests head


    【解决方案1】:

    如果您查看返回的标头,您会注意到:

    Transfer-Encoding: chunked

    但是,如果您使用 httpbin 比较发送的标头,您可能会注意到 Requests 还发送 Accept-Encoding: gzip, deflate。如果您删除该标题,例如,

    r = requests.head(url, headers={'Accept-Encoding': None})

    然后您将获得 Content-Length 标头。

    来源:https://github.com/psf/requests/issues/3953

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 2016-08-29
      • 2019-10-16
      • 2017-08-14
      • 2018-04-12
      • 2016-06-05
      相关资源
      最近更新 更多