httpx HTTP 代理

2022-07-26 14:04 更新

HTTPX支持通过​proxies​参数设置 HTTP 代理,该参数将在client初始化或顶级API函数(如HTTPX)上传递。

要将所有流量(HTTP 和 HTTPS)路由到位于 ​http://localhost:8030​的代理,请将代理 URL 传递给​Client​...

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

对于更高级的用例,请传递代理​dict​(dict为python的字典类型,也可翻译为字典,此处不做翻译)。例如,要将 HTTP 和 HTTPS 请求路由到 2 个不同的代理,分别位于​http://localhost:8030 ​和​http://localhost:8031​ ,请传递代理 URL 的​dict​:

proxies = {
    "http://": "http://localhost:8030",
    "https://": "http://localhost:8031",
}

with httpx.Client(proxies=proxies) as client:
    ...

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

补充:
在大多数情况下,​https://key​的代理URL应该使用​http://scheme​(这不是拼写错误!)。
这是因为HTTP代理需要启动与代理服务器的连接。虽然您的代理可能支持通过HTTPS执行,但大多数代理仅支持通过HTTP执行。
有关详细信息,请参阅本章节的 转发与隧道。

认证

代理凭据可以作为代理 URL 的​userinfo​部分传递。例如:

proxies = {
    "http://": "http://username:password@localhost:8030",
    # ...
}

路由

HTTPX提供了细粒度的控制,用于决定哪些请求可以通过代理,哪些不可以通过代理。此过程称为代理路由。

代理字典将URL模式(“代理密钥”)映射到代理URL。HTTPX将请求的URL与代理密钥进行匹配,以决定应该使用哪个代理(如果有的话)。从最特定的代理密钥(例如​https://<domain>:<port>​)到最不特定的代理密钥(例如​https:///​)进行匹配。

HTTPX 支持基于scheme、域、端口或这些方案的组合的路由代理。

通配符路由

通过代理路由所有内容...

proxies = {
    "all://": "http://localhost:8030",
}

方案路由

通过一个代理路由HTTP请求,通过另一个代理路由HTTPS请求...

proxies = {
    "http://": "http://localhost:8030",
    "https://": "http://localhost:8031",
}

域路由

代理域“example.com”上的所有请求,让其他请求通过...

proxies = {
    "all://example.com": "http://localhost:8030",
}

代理HTTP请求在域“example.com”上,让HTTPS和其他请求通过...

proxies = {
    "http://example.com": "http://localhost:8030",
}

代理所有请求到“example.com”及其子域,让其他请求通过...

proxies = {
    "all://*example.com": "http://localhost:8030",
}

将所有请求代理到“example.com”的严格子域,让“example.com”等请求通过...

proxies = {
    "all://*.example.com": "http://localhost:8030",
}

端口路由

在端口1234上代理HTTPS请求到“example.com”...

proxies = {
    "https://example.com:1234": "http://localhost:8030",
}

代理端口 1234 上的所有请求...

proxies = {
    "all://*:1234": "http://localhost:8030",
}

无代理支持

还可以定义不应通过代理路由的请求。

为此,请传递None作为代理URL。例如...

proxies = {
    # Route requests through a proxy by default...
    "all://": "http://localhost:8031",
    # Except those for "example.com".
    "all://example.com": None,
}

复杂配置示例

您可以组合上述路由功能来构建复杂的代理路由配置。例如。。。

proxies = {
    # Route all traffic through a proxy by default...
    "all://": "http://localhost:8030",
    # But don't use proxies for HTTPS requests to "domain.io"...
    "https://domain.io": None,
    # And use another proxy for requests to "example.com" and its subdomains...
    "all://*example.com": "http://localhost:8031",
    # And yet another proxy if HTTP is used,
    # and the "internal" subdomain on port 5550 is requested...
    "http://internal.example.com:5550": "http://localhost:8032",
}

环境变量

HTTP代理也可以通过环境变量进行配置,尽管其细粒度控制较少。

有关详细信息,请参阅有关 HTTP_PROXY,HTTPS_PROXY,ALL_PROXY的文档。

代理机制

注意
本节介绍高级代理概念和功能。

转发与隧道

通常,通过代理发出 HTTP 请求的流程如下:

  1. 客户端连接到代理(初始连接请求)。
  2. 代理代表您将数据传输到服务器。

步骤 2 的确切执行方式取决于使用两种代理机制中的哪一种:

  • 转发:代理为您发出请求,并发回从服务器获得的响应。
  • 隧道:代理代表您与服务器建立 TCP 连接,客户端重用此连接来发送请求并接收响应。这称为 HTTP 隧道。此机制是您可以从 HTTP 代理访问使用 HTTPS 的网站的方式(客户端通过代理提供的 TCP 连接与服务器执行 TLS 握手来“升级”到 HTTPS 的连接)。

代理疑难解答

如果您在设置代理时遇到问题,请参阅我们的疑难解答指南


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号