environment_variables.md
This file
Path
docs/environment_variables.md
113 lines · 3.7 KiB · Markdown
Last changed by
Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
History of this fileTake it
The walk stopped after 200 commits: lines older than 28ed42e point at it.
Bump twine from 4.0.0 to 4.0.1 (#2256)dependabot[bot]before adoption28ed42e
lines 1–113why, one step back1# Environment Variables23The HTTPX library can be configured via environment variables.4Environment 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:56* On the client via `httpx.Client(trust_env=False)`.7* Using the top-level API, such as `httpx.get("<url>", trust_env=False)`.89Here is a list of environment variables that HTTPX recognizes and what function they serve:1011## `SSLKEYLOGFILE`1213Valid values: a filename1415If this environment variable is set, TLS keys will be appended to the specified file, creating it if it doesn't exist, whenever key material is generated or received. The keylog file is designed for debugging purposes only.1617Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer.1819Example:2021```python22# test_script.py23import httpx2425with httpx.AsyncClient() as client:26 r = client.get("https://google.com")27```2829```console30SSLKEYLOGFILE=test.log python test_script.py31cat test.log32# TLS secrets log file, generated by OpenSSL / Python33SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX34EXPORTER_SECRET XXXX35SERVER_TRAFFIC_SECRET_0 XXXX36CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX37CLIENT_TRAFFIC_SECRET_0 XXXX38SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX39EXPORTER_SECRET XXXX40SERVER_TRAFFIC_SECRET_0 XXXX41CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX42CLIENT_TRAFFIC_SECRET_0 XXXX43```4445## `SSL_CERT_FILE`4647Valid values: a filename4849If this environment variable is set then HTTPX will load50CA certificate from the specified file instead of the default51location.5253Example:5455```console56SSL_CERT_FILE=/path/to/ca-certs/ca-bundle.crt python -c "import httpx; httpx.get('https://example.com')"57```5859## `SSL_CERT_DIR`6061Valid values: a directory following an [OpenSSL specific layout](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_load_verify_locations.html).6263If this environment variable is set and the directory follows an [OpenSSL specific layout](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_load_verify_locations.html) (ie. you ran `c_rehash`) then HTTPX will load CA certificates from this directory instead of the default location.6465Example:6667```console68SSL_CERT_DIR=/path/to/ca-certs/ python -c "import httpx; httpx.get('https://example.com')"69```7071## Proxies7273The environment variables documented below are used as a convention by various HTTP tooling, including:7475* [cURL](https://github.com/curl/curl/blob/master/docs/MANUAL.md#environment-variables)76* [requests](https://github.com/psf/requests/blob/master/docs/user/advanced.rst#proxies)7778For more information on using proxies in HTTPX, see [HTTP Proxying](advanced.md#http-proxying).7980### `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`8182Valid values: A URL to a proxy8384`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY` set the proxy to be used for `http`, `https`, or all requests respectively.8586```bash87export HTTP_PROXY=http://my-external-proxy.com:12348889# This request will be sent through the proxy90python -c "import httpx; httpx.get('http://example.com')"9192# This request will be sent directly, as we set `trust_env=False`93python -c "import httpx; httpx.get('http://example.com', trust_env=False)"9495```9697### `NO_PROXY`9899Valid values: a comma-separated list of hostnames/urls100101`NO_PROXY` disables the proxy for specific urls102103```bash104export HTTP_PROXY=http://my-external-proxy.com:1234105export NO_PROXY=http://127.0.0.1,python-httpx.org106107# As in the previous example, this request will be sent through the proxy108python -c "import httpx; httpx.get('http://example.com')"109110# These requests will be sent directly, bypassing the proxy111python -c "import httpx; httpx.get('http://127.0.0.1:5000/my-api')"112python -c "import httpx; httpx.get('https://www.python-httpx.org')"113```