Python 3.x如何获取http版本(使用请求库)

use*_*259 2 python-3.x python-requests

最好使用请求库

HTTP/1.1 200 OK        <-- I want this...
Content-Type: text/html; charset=utf-8
Run Code Online (Sandbox Code Playgroud)

似乎没有提供该属性http://docs.python-requests.org/en/master/api/#requests.Response

有没有办法访问原始响应字符串?

我找到了http://docs.python-requests.org/en/master/user/quickstart/#raw-response-content 但我没有看到任何内容

r = requests.head(uri, stream=True)
print(r.raw.read(10)) # -> b''
Run Code Online (Sandbox Code Playgroud)

J91*_*321 8

我想你想要的是这个.在raw上调用版本将为您提供HTTP版本.(我发现服务器运行HTTP 1.0的示例使用Shodan进行测试)

>>> import requests
>>> response = requests.get("http://104.71.136.252/", timeout=60, verify=False)
>>> response.raw.version
10
>>> response = requests.get("http://stackoverflow.com", timeout=60, verify=False)
>>> response.raw.version
11
Run Code Online (Sandbox Code Playgroud)

直接在文档中没有提到这一点,我通过使用PyCharm的自动完成功能找到了它.但我已经调查过了.HTTP版本作为整数返回的原因是历史的.

python3的请求使用urllib3.Urllib3的类urllib3.response.HTTPResponse向后兼容httplib的HTTPResponse,请参阅urllib3 docs

现在,如果它向后兼容,你必须检查httplib的文档,如果你搜索你会发现

服务器使用的HTTP协议版本.10表示HTTP/1.0,11表示HTTP/1.1.

这是HTTPResponse.version的确切链接