HTTP/2
¥HTTP/2
HTTP/2 是 HTTP 协议的一个重要的迭代版本,它提供了更高效的传输方式,并具有潜在的性能优势。HTTP/2 不会改变请求或响应的核心语义,但会改变数据与服务器之间的收发方式。
¥HTTP/2 is a major new iteration of the HTTP protocol, that provides a far more efficient transport, with potential performance benefits. HTTP/2 does not change the core semantics of the request or response, but alters the way that data is sent to and from the server.
HTTP/2 采用二进制格式,而非 HTTP/1.1 所用的文本格式。二进制格式提供完整的请求和响应复用功能,并高效压缩 HTTP 标头。流复用意味着,HTTP/1.1 要求每个并发请求使用一个 TCP 流,而 HTTP/2 允许单个 TCP 流处理多个并发请求。
¥Rather than the text format that HTTP/1.1 uses, HTTP/2 is a binary format. The binary format provides full request and response multiplexing, and efficient compression of HTTP headers. The stream multiplexing means that where HTTP/1.1 requires one TCP stream for each concurrent request, HTTP/2 allows a single TCP stream to handle multiple concurrent requests.
HTTP/2 还提供对响应优先级和服务器推送等功能的支持。
¥HTTP/2 also provides support for functionality such as response prioritization, and server push.
有关 HTTP/2 的全面指南,您可能需要查看“http2 详解“。
¥For a comprehensive guide to HTTP/2 you may want to check out "http2 explained".
启用 HTTP/2
¥Enabling HTTP/2
使用时httpx客户端默认不启用 HTTP/2 支持,因为 HTTP/1.1 是一个成熟的、久经考验的传输层,而我们的 HTTP/1.1 实现目前可能被认为是更健壮的选择。未来版本的httpx可能默认启用 HTTP/2 支持。
¥When using the httpx client, HTTP/2 support is not enabled by default, because
HTTP/1.1 is a mature, battle-hardened transport layer, and our HTTP/1.1
implementation may be considered the more robust option at this point in time.
It is possible that a future version of httpx may enable HTTP/2 support by default.
如果您需要发出高并发请求,不妨考虑试用我们的 HTTP/2 支持。您可以先确保安装可选的 HTTP/2 依赖项……
¥If you're issuing highly concurrent requests you might want to consider trying out our HTTP/2 support. You can do so by first making sure to install the optional HTTP/2 dependencies...
$ pip install httpx[http2]
然后实例化一个启用了 HTTP/2 支持的客户端:
¥And then instantiating a client with HTTP/2 support enabled:
client = httpx.AsyncClient(http2=True)
...
您还可以将客户端实例化为上下文管理器,以确保所有 HTTP 连接都具有良好的范围,并且在退出上下文块后将关闭。
¥You can also instantiate a client as a context manager, to ensure that all HTTP connections are nicely scoped, and will be closed once the context block is exited.
async with httpx.AsyncClient(http2=True) as client:
...
两者均可支持 HTTP/2Client和AsyncClient,但如果您发出大量并发请求,它通常在异步上下文中更有用。
¥HTTP/2 support is available on both Client and AsyncClient, although it's
typically more useful in async contexts if you're issuing lots of concurrent
requests.
检查 HTTP 版本
¥Inspecting the HTTP version
在客户端上启用 HTTP/2 支持不会一定意味着你的请求和响应将通过 HTTP/2 传输,因为客户端和服务器需要支持 HTTP/2。如果您连接到仅支持 HTTP/1.1 的服务器,客户端将改用标准 HTTP/1.1 连接。
¥Enabling HTTP/2 support on the client does not necessarily mean that your requests and responses will be transported over HTTP/2, since both the client and the server need to support HTTP/2. If you connect to a server that only supports HTTP/1.1 the client will use a standard HTTP/1.1 connection instead.
您可以通过检查以下信息来确定所使用的 HTTP 协议版本:.http_version响应的属性。
¥You can determine which version of the HTTP protocol was used by examining
the .http_version property on the response.
client = httpx.AsyncClient(http2=True)
response = await client.get(...)
print(response.http_version) # "HTTP/1.0", "HTTP/1.1", or "HTTP/2".