简体   繁体   English

Python 请求:缺少内容长度响应

[英]Python requests: missing content-length response

Python requests does not returns the content-length header what I can get with curl. Python 请求不返回内容长度 header 我可以使用 curl 获得的内容。

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

Response:回复:

{'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'}

I also tried with requests.get, but also not working: response = requests.get(url, stream=True)我也尝试了 requests.get,但也没有工作: response = requests.get(url, stream=True)

If you look at the headers that are returned, you will notice:如果您查看返回的标头,您会注意到:

Transfer-Encoding: chunked

If you compare the headers that are sent, however, using httpbin, you might notice that Requests also sends Accept-Encoding: gzip, deflate.但是,如果您使用 httpbin 比较发送的标头,您可能会注意到 Requests 还发送 Accept-Encoding: gzip, deflate。 If you remove that header, eg,如果删除 header,例如,

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

Then you get your Content-Length header.然后你得到你的内容长度 header。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM