python 2.7 中的 HTTP 2 请求
HTTP 2 request in python 2.7
在python中向HTTP/1和HTTP/2提出请求有什么不同吗?
我可以在 python 中进行 HTTP/1.x 调用,例如
url = 'http://someURL'
values = {'param1' : 'key',
'param2' : 'key2'}
data = urllib.urlencode(values)
print data
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
默认情况下 python 支持制作 HTTP/2 还是我应该添加任何额外的东西。
正如其他人在对问题的评论中提到的,requests
库不支持 HTTP/2。
来自 requests
图书馆 documentation:
Requests allows you to send organic, grass-fed HTTP/1.1 requests, without the need for manual labor.
到目前为止,我所知道的 Python 的唯一 HTTP/2 客户端是 hyper
,引用自文档:
supports Python 3.4 and Python 2.7.9, and can speak HTTP/2 and HTTP/1.1
作为参考,截至 2019 年,另一个支持 HTTP/2 的库是 HTTPX。
HTTPX is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.
这至少需要 Python 3.6。但是,在 2020 年撰写本文时,Python 2 已经停产,因此 Python 3.6 对任何用户来说都应该没问题。
将 hyper
与 requests
模块一起使用。
import requests
from hyper.contrib import HTTP20Adapter
s = requests.Session()
s.mount('https://http2bin.org', HTTP20Adapter())
r = s.get('https://http2bin.org/get')
print(r.status_code)
https://hyper.readthedocs.io/en/latest/quickstart.html#requests-integration
在python中向HTTP/1和HTTP/2提出请求有什么不同吗?
我可以在 python 中进行 HTTP/1.x 调用,例如
url = 'http://someURL'
values = {'param1' : 'key',
'param2' : 'key2'}
data = urllib.urlencode(values)
print data
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
默认情况下 python 支持制作 HTTP/2 还是我应该添加任何额外的东西。
正如其他人在对问题的评论中提到的,requests
库不支持 HTTP/2。
来自 requests
图书馆 documentation:
Requests allows you to send organic, grass-fed HTTP/1.1 requests, without the need for manual labor.
到目前为止,我所知道的 Python 的唯一 HTTP/2 客户端是 hyper
,引用自文档:
supports Python 3.4 and Python 2.7.9, and can speak HTTP/2 and HTTP/1.1
作为参考,截至 2019 年,另一个支持 HTTP/2 的库是 HTTPX。
HTTPX is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.
这至少需要 Python 3.6。但是,在 2020 年撰写本文时,Python 2 已经停产,因此 Python 3.6 对任何用户来说都应该没问题。
将 hyper
与 requests
模块一起使用。
import requests
from hyper.contrib import HTTP20Adapter
s = requests.Session()
s.mount('https://http2bin.org', HTTP20Adapter())
r = s.get('https://http2bin.org/get')
print(r.status_code)
https://hyper.readthedocs.io/en/latest/quickstart.html#requests-integration