Python请求: missing content-length response

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

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

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)

最佳答案

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

传输编码:分块

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

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

然后您将获得 Content-Length header 。

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

关于Python请求: missing content-length response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61991164/

相关文章:

python - 使用 python 将 JSON 对象写入文件

python - 是什么导致此 djcelery 错误 : NotRegistered?

python - Matplotlib 包含文件中的数据

python - 由于 http 405 错误,使用 POST 从网站自动检索数据无法正常工作

python - 有没有更简洁的方法来构建这个列表? python 3

python - plotly :如何防止标题与 plotly 重叠?

python - Python 3.6 及更早版本的精确时间(纳秒)?

javascript - 使用 axios get 获取请求失败,状态代码为 403

ruby-on-rails - 在 Rails 中使用 `JSON.decode` 的结果

algorithm - Restful : Using POST to execute algorithms