Skip to content

代理

¥Proxies

HTTPX 支持设置HTTP 代理通过proxy在客户端初始化或顶级 API 函数中传递的参数,例如httpx.get(..., proxy=...)

¥HTTPX supports setting up HTTP proxies via the proxy parameter to be passed on client initialization or top-level API functions like httpx.get(..., proxy=...).

Diagram of how a proxy works (source: Wikipedia). The left hand side "Internet" blob may be your HTTPX client requesting example.com through a proxy.

HTTP 代理

¥HTTP Proxies

将所有流量(HTTP 和 HTTPS)路由到位于http://localhost:8030,将代理 URL 传递给客户端...

¥To route all traffic (HTTP and HTTPS) to a proxy located at http://localhost:8030, pass the proxy URL to the client...

with httpx.Client(proxy="http://localhost:8030") as client:
    ...

对于更高级的用例,传递 mountsdict。例如,将 HTTP 和 HTTPS 请求路由到 2 个不同的代理,分别位于http://localhost:8030, 和http://localhost:8031,通过dict代理 URL 的数量:

¥For more advanced use cases, pass a mounts dict. For example, to route HTTP and HTTPS requests to 2 different proxies, respectively located at http://localhost:8030, and http://localhost:8031, pass a dict of proxy URLs:

proxy_mounts = {
    "http://": httpx.HTTPTransport(proxy="http://localhost:8030"),
    "https://": httpx.HTTPTransport(proxy="http://localhost:8031"),
}

with httpx.Client(mounts=proxy_mounts) as client:
    ...

有关代理路由的详细信息,请参阅路由部分。

¥For detailed information about proxy routing, see the Routing section.

陷阱

¥Gotcha

在大多数情况下,https://钥匙应该使用http://方案(这不是打字错误!)。

¥In most cases, the proxy URL for the https:// key should use the http:// scheme (that's not a typo!).

这是因为 HTTP 代理需要与代理服务器建立连接。虽然您的代理服务器可能支持通过 HTTPS 进行连接,但大多数代理服务器仅支持通过 HTTP 进行连接。

¥This is because HTTP proxying requires initiating a connection with the proxy server. While it's possible that your proxy supports doing it via HTTPS, most proxies only support doing it via HTTP.

有关详细信息,请参阅前进与隧道

¥For more information, see FORWARD vs TUNNEL.

验证

¥Authentication

代理凭证可以作为userinfo代理 URL 部分。例如:

¥Proxy credentials can be passed as the userinfo section of the proxy URL. For example:

with httpx.Client(proxy="http://username:password@localhost:8030") as client:
    ...

代理机制

¥Proxy mechanisms

笔记

¥Note

本节介绍先进的代理概念和功能。

¥This section describes advanced proxy concepts and functionality.

前进与隧道

¥FORWARD vs TUNNEL

一般来说,通过代理发出 HTTP 请求的流程如下:

¥In general, the flow for making an HTTP request through a proxy is as follows:

  1. 客户端连接到代理(初始连接请求)。

    ¥The client connects to the proxy (initial connection request).

  2. 代理代表您将数据传输到服务器。

    ¥The proxy transfers data to the server on your behalf.

步骤 2/ 具体如何执行取决于使用哪种代理机制:

¥How exactly step 2/ is performed depends on which of two proxying mechanisms is used:

  • 转发:代理为您发出请求,并发回从服务器获得的响应。

    ¥Forwarding: the proxy makes the request for you, and sends back the response it obtained from the server.

  • 隧道施工:代理代表您与服务器建立 TCP 连接,客户端重用此连接发送请求并接收响应。这被称为HTTP 隧道。此机制就是您如何从 HTTP 代理访问使用 HTTPS 的网站(客户端通过代理提供的 TCP 连接与服务器执行 TLS 握手,将连接“升级”为 HTTPS)。

    ¥Tunnelling: the proxy establishes a TCP connection to the server on your behalf, and the client reuses this connection to send the request and receive the response. This is known as an HTTP Tunnel. This mechanism is how you can access websites that use HTTPS from an HTTP proxy (the client "upgrades" the connection to HTTPS by performing the TLS handshake with the server over the TCP connection provided by the proxy).

代理故障排除

¥Troubleshooting proxies

如果您在设置代理时遇到问题,请参阅我们的故障排除指南

¥If you encounter issues when setting up proxies, please refer to our Troubleshooting guide.

袜子

¥SOCKS

除了 HTTP 代理之外,httpcore还支持使用 SOCKS 协议的代理。这是一个可选功能,使用前需要安装额外的第三方库。

¥In addition to HTTP proxies, httpcore also supports proxies using the SOCKS protocol. This is an optional feature that requires an additional third-party library be installed before use.

您可以使用以下方式安装 SOCKS 支持pip

¥You can install SOCKS support using pip:

$ pip install httpx[socks]

您现在可以配置客户端使用 SOCKS 协议通过代理发出请求:

¥You can now configure a client to make requests via a proxy using the SOCKS protocol:

httpx.Client(proxy='socks5://user:pass@host:port')