Skip to content

环境变量

¥Environment Variables

HTTPX 库可以通过环境变量进行配置。默认情况下使用环境变量。要忽略环境变量,trust_env必须设置False. 有两种设置方法trust_env禁用环境变量:

¥The HTTPX library can be configured via environment variables. Environment variables are used by default. To ignore environment variables, trust_env has to be set False. There are two ways to set trust_env to disable environment variables:

  • 在客户端上通过httpx.Client(trust_env=False)

    ¥On the client via httpx.Client(trust_env=False).

  • 使用顶级 API,例如httpx.get("<url>", trust_env=False)

    ¥Using the top-level API, such as httpx.get("<url>", trust_env=False).

以下是 HTTPX 识别的环境变量列表以及它们所起的作用:

¥Here is a list of environment variables that HTTPX recognizes and what function they serve:

代理

¥Proxies

下面记录的环境变量被各种 HTTP 工具用作约定,包括:

¥The environment variables documented below are used as a convention by various HTTP tooling, including:

有关在 HTTPX 中使用代理的更多信息,请参阅HTTP 代理

¥For more information on using proxies in HTTPX, see HTTP Proxying.

HTTP_PROXYHTTPS_PROXYALL_PROXY

¥HTTP_PROXY, HTTPS_PROXY, ALL_PROXY

有效值:代理的 URL

¥Valid values: A URL to a proxy

HTTP_PROXYHTTPS_PROXYALL_PROXY设置要使用的代理httphttps或所有请求。

¥HTTP_PROXY, HTTPS_PROXY, ALL_PROXY set the proxy to be used for http, https, or all requests respectively.

export HTTP_PROXY=http://my-external-proxy.com:1234

# This request will be sent through the proxy
python -c "import httpx; httpx.get('http://example.com')"

# This request will be sent directly, as we set `trust_env=False`
python -c "import httpx; httpx.get('http://example.com', trust_env=False)"

NO_PROXY

¥NO_PROXY

有效值:以逗号分隔的主机名/URL 列表

¥Valid values: a comma-separated list of hostnames/urls

NO_PROXY禁用特定 URL 的代理

¥NO_PROXY disables the proxy for specific urls

export HTTP_PROXY=http://my-external-proxy.com:1234
export NO_PROXY=http://127.0.0.1,python-httpx.org

# As in the previous example, this request will be sent through the proxy
python -c "import httpx; httpx.get('http://example.com')"

# These requests will be sent directly, bypassing the proxy
python -c "import httpx; httpx.get('http://127.0.0.1:5000/my-api')"
python -c "import httpx; httpx.get('https://httpx.iloveaiwork.com')"