HTTPX
¥ HTTPX
HTTPX 是 Python 3 的全功能 HTTP 客户端,它提供同步和异步 API,并支持 HTTP/1.1 和 HTTP/2。
¥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.
使用 pip 安装 HTTPX:
¥Install HTTPX using pip:
$ pip install httpx
现在,让我们开始吧:
¥Now, let's get started:
>>> import httpx
>>> r = httpx.get('https://www.example.org/')
>>> r
<Response [200 OK]>
>>> r.status_code
200
>>> r.headers['content-type']
'text/html; charset=UTF-8'
>>> r.text
'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'
或者,使用命令行客户端。
¥Or, using the command-line client.
# The command line client is an optional dependency.
$ pip install 'httpx[cli]'
现在我们可以直接从命令行使用 HTTPX...
¥Which now allows us to use HTTPX directly from the command-line...

正在发送请求...
¥Sending a request...

特征
¥Features
HTTPX 建立在以下成熟可用性基础之上:requests,并给出:
¥HTTPX builds on the well-established usability of requests, and gives you:
广义上请求兼容 API。
¥A broadly requests-compatible API.
标准同步接口,但具有如果需要的话,异步支持。
¥Standard synchronous interface, but with async support if you need it.
HTTP/1.1以及 HTTP/2 支持。
¥HTTP/1.1 and HTTP/2 support.
¥Ability to make requests directly to WSGI applications or ASGI applications.
到处都有严格的超时规定。
¥Strict timeouts everywhere.
完整类型注释。
¥Fully type annotated.
100%测试覆盖率。
¥100% test coverage.
加上所有标准功能requests...
¥Plus all the standard features of requests...
国际域名和 URL
¥International Domains and URLs
保持活动和连接池
¥Keep-Alive & Connection Pooling
具有 Cookie 持久性的会话
¥Sessions with Cookie Persistence
浏览器风格的 SSL 验证
¥Browser-style SSL Verification
基本/摘要式身份验证
¥Basic/Digest Authentication
优雅的键/值 Cookie
¥Elegant Key/Value Cookies
自动减压
¥Automatic Decompression
自动内容解码
¥Automatic Content Decoding
Unicode 响应主体
¥Unicode Response Bodies
分段文件上传
¥Multipart File Uploads
HTTP(S) 代理支持
¥HTTP(S) Proxy Support
连接超时
¥Connection Timeouts
流媒体下载
¥Streaming Downloads
.netrc 支持
¥.netrc Support
分块请求
¥Chunked Requests
文档
¥Documentation
要了解所有基础知识,请前往快速入门。
¥For a run-through of all the basics, head over to the QuickStart.
有关更高级的主题,请参阅先进的部分,异步支持部分,或HTTP/2部分。
¥For more advanced topics, see the Advanced section, the async support section, or the HTTP/2 section.
这开发者界面提供全面的API参考。
¥The Developer Interface provides a comprehensive API reference.
要了解与 HTTPX 集成的工具,请参阅第三方软件包。
¥To find out about tools that integrate with HTTPX, see Third Party Packages.
依赖项
¥Dependencies
HTTPX 项目依赖于这些优秀的库:
¥The HTTPX project relies on these excellent libraries:
httpcore- 底层传输实现httpx。¥
httpcore- The underlying transport implementation forhttpx.h11- HTTP/1.1 支持。¥
h11- HTTP/1.1 support.certifi- SSL 证书。¥
certifi- SSL certificates.idna- 国际化域名支持。¥
idna- Internationalized domain name support.sniffio- 异步库自动检测。¥
sniffio- Async library autodetection.
以及这些可选安装:
¥As well as these optional installs:
h2-HTTP/2 支持。(可选,httpx[http2])¥
h2- HTTP/2 support. (Optional, withhttpx[http2])socksio- SOCKS代理支持。(可选,httpx[socks])¥
socksio- SOCKS proxy support. (Optional, withhttpx[socks])rich- 丰富的终端支持。(可选,httpx[cli])¥
rich- Rich terminal support. (Optional, withhttpx[cli])click- 命令行客户端支持。(可选,httpx[cli])¥
click- Command line client support. (Optional, withhttpx[cli])brotli或者brotlicffi- 解码“brotli”压缩响应。(可选,httpx[brotli])¥
brotliorbrotlicffi- Decoding for "brotli" compressed responses. (Optional, withhttpx[brotli])zstandard- 解码“zstd”压缩响应。(可选,httpx[zstd])¥
zstandard- Decoding for "zstd" compressed responses. (Optional, withhttpx[zstd])
很大一部分功劳归功于requests对于大部分工作遵循的 API 布局,以及urllib3围绕底层网络细节获取大量设计灵感。
¥A huge amount of credit is due to requests for the API layout that
much of this work follows, as well as to urllib3 for plenty of design
inspiration around the lower-level networking details.
安装
¥Installation
使用 pip 安装:
¥Install with pip:
$ pip install httpx
或者,要包含可选的 HTTP/2 支持,请使用:
¥Or, to include the optional HTTP/2 support, use:
$ pip install httpx[http2]
要包含可选的 brotli 和 zstandard 解码器支持,请使用:
¥To include the optional brotli and zstandard decoders support, use:
$ pip install httpx[brotli,zstd]
HTTPX 需要 Python 3.9+
¥HTTPX requires Python 3.9+