environment_variables.md
This file
Path
docs/environment_variables.md
177 lines · 11.1 KiB · Markdown
Last changed by
Explain SSL_CERT_DIR specific format (#1470)Quentin Pradetbefore adoption645ae4e
History of this fileTake it
The walk stopped after 200 commits: lines older than a89d4ad point at it.
Use shebang-style headers in environment variables docs (#1260)Florimond Mancabefore adoption98bb548
line 1why, one step back1# Environment VariablesMore public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 2–10why, one step back23The 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:10Use shebang-style headers in environment variables docs (#1260)Florimond Mancabefore adoption98bb548
line 11why, one step back11## `HTTPX_LOG_LEVEL`More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 12–74why, one step back1213Valid values: `debug`, `trace` (case-insensitive)1415If set to `debug`, then HTTP requests will be logged to `stderr`. This is useful for general purpose reporting of network activity.1617If set to `trace`, then low-level details about the execution of HTTP requests will be logged to `stderr`, in addition to debug log lines. This can help you debug issues and see what's exactly being sent over the wire and to which location.1819Example:2021```python22# test_script.py23import httpx2425with httpx.Client() as client:26 r = client.get("https://google.com")27```2829Debug output:3031```console32$ HTTPX_LOG_LEVEL=debug python test_script.py33DEBUG [2019-11-06 19:11:24] httpx._client - HTTP Request: GET https://google.com "HTTP/1.1 301 Moved Permanently"34DEBUG [2019-11-06 19:11:24] httpx._client - HTTP Request: GET https://www.google.com/ "HTTP/1.1 200 OK"35```3637Trace output:3839```console40$ HTTPX_LOG_LEVEL=trace python test_script.py41TRACE [2019-11-06 19:18:56] httpx._dispatch.connection_pool - acquire_connection origin=Origin(scheme='https' host='google.com' port=443)42TRACE [2019-11-06 19:18:56] httpx._dispatch.connection_pool - new_connection connection=HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))43TRACE [2019-11-06 19:18:56] httpx._config - load_ssl_context verify=True cert=None trust_env=True http_versions=HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])44TRACE [2019-11-06 19:18:56] httpx._config - load_verify_locations cafile=/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.8/site-packages/certifi/cacert.pem45TRACE [2019-11-06 19:18:56] httpx._dispatch.connection - start_connect host='google.com' port=443 timeout=Timeout(timeout=5.0)46TRACE [2019-11-06 19:18:56] httpx._dispatch.connection - connected http_version='HTTP/2'47TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - send_headers stream_id=1 method='GET' target='/' headers=[(b':method', b'GET'), (b':authority', b'google.com'), (b':scheme', b'https'), (b':path', b'/'), (b'user-agent', b'python-httpx/0.7.6'), (b'accept', b'*/*'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive')]48TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - end_stream stream_id=149TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<RemoteSettingsChanged changed_settings:{ChangedSetting(setting=SettingCodes.MAX_CONCURRENT_STREAMS, original_value=None, new_value=100), ChangedSetting(setting=SettingCodes.INITIAL_WINDOW_SIZE, original_value=65535, new_value=1048576), ChangedSetting(setting=SettingCodes.MAX_HEADER_LIST_SIZE, original_value=None, new_value=16384)}>50TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<WindowUpdated stream_id:0, delta:983041>51TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<SettingsAcknowledged changed_settings:{}>52TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=1 event=<ResponseReceived stream_id:1, headers:[(b':status', b'301'), (b'location', b'https://www.google.com/'), (b'content-type', b'text/html; charset=UTF-8'), (b'date', b'Wed, 06 Nov 2019 18:18:56 GMT'), (b'expires', b'Fri, 06 Dec 2019 18:18:56 GMT'), (b'cache-control', b'public, max-age=2592000'), (b'server', b'gws'), (b'content-length', b'220'), (b'x-xss-protection', b'0'), (b'x-frame-options', b'SAMEORIGIN'), (b'alt-svc', b'quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000')]>53DEBUG [2019-11-06 19:18:56] httpx._client - HTTP Request: GET https://google.com "HTTP/2 301 Moved Permanently"54TRACE [2019-11-06 19:18:56] httpx._dispatch.connection_pool - acquire_connection origin=Origin(scheme='https' host='www.google.com' port=443)55TRACE [2019-11-06 19:18:56] httpx._dispatch.connection_pool - new_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))56TRACE [2019-11-06 19:18:56] httpx._config - load_ssl_context verify=True cert=None trust_env=True http_versions=HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])57TRACE [2019-11-06 19:18:56] httpx._config - load_verify_locations cafile=/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.8/site-packages/certifi/cacert.pem58TRACE [2019-11-06 19:18:56] httpx._dispatch.connection - start_connect host='www.google.com' port=443 timeout=Timeout(timeout=5.0)59TRACE [2019-11-06 19:18:56] httpx._dispatch.connection - connected http_version='HTTP/2'60TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - send_headers stream_id=1 method='GET' target='/' headers=[(b':method', b'GET'), (b':authority', b'www.google.com'), (b':scheme', b'https'), (b':path', b'/'), (b'user-agent', b'python-httpx/0.7.6'), (b'accept', b'*/*'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive')]61TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - end_stream stream_id=162TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<RemoteSettingsChanged changed_settings:{ChangedSetting(setting=SettingCodes.MAX_CONCURRENT_STREAMS, original_value=None, new_value=100), ChangedSetting(setting=SettingCodes.INITIAL_WINDOW_SIZE, original_value=65535, new_value=1048576), ChangedSetting(setting=SettingCodes.MAX_HEADER_LIST_SIZE, original_value=None, new_value=16384)}>63TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<WindowUpdated stream_id:0, delta:983041>64TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<SettingsAcknowledged changed_settings:{}>65TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=1 event=<ResponseReceived stream_id:1, headers:[(b':status', b'200'), (b'date', b'Wed, 06 Nov 2019 18:18:56 GMT'), (b'expires', b'-1'), (b'cache-control', b'private, max-age=0'), (b'content-type', b'text/html; charset=ISO-8859-1'), (b'p3p', b'CP="This is not a P3P policy! See g.co/p3phelp for more info."'), (b'content-encoding', b'gzip'), (b'server', b'gws'), (b'content-length', b'5073'), (b'x-xss-protection', b'0'), (b'x-frame-options', b'SAMEORIGIN'), (b'set-cookie', b'1P_JAR=2019-11-06-18; expires=Fri, 06-Dec-2019 18:18:56 GMT; path=/; domain=.google.com; SameSite=none'), (b'set-cookie', b'NID=190=m8G9qLxCz2_4HbZI02ON2HTJF4xTvOhoJiS57Hm-OJrNS2eY20LfXMR_u-mLjujeshW5-BTezI69OGpHksT4ZK2TCDsWeU0DF7AmDTjjXFOdj30eIUTpNq7r9aWRvI8UrqiwlIsLkE8Ee3t5PiIiVdSMUcji7dkavGlMUpkMXU8; expires=Thu, 07-May-2020 18:18:56 GMT; path=/; domain=.google.com; HttpOnly'), (b'alt-svc', b'quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000')]>66DEBUG [2019-11-06 19:18:56] httpx._client - HTTP Request: GET https://www.google.com/ "HTTP/2 200 OK"67TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=1 event=<DataReceived stream_id:1, flow_controlled_length:5186, data:1f8b08000000000002ffc55af97adb4692ff3f4f>68TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=1 event=<DataReceived stream_id:1, flow_controlled_length:221, data:>69TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=1 event=<StreamEnded stream_id:1>70TRACE [2019-11-06 19:18:56] httpx._dispatch.http2 - receive_event stream_id=0 event=<PingReceived ping_data:0000000000000000>71TRACE [2019-11-06 19:18:56] httpx._dispatch.connection_pool - release_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))72TRACE [2019-11-06 19:18:56] httpx._dispatch.connection - close_connection73```74Use shebang-style headers in environment variables docs (#1260)Florimond Mancabefore adoption98bb548
line 75why, one step back75## `SSLKEYLOGFILE`More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 76–108why, one step back7677Valid values: a filename7879If 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.8081Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer.8283Example:8485```python86# test_script.py87import httpx8889with httpx.AsyncClient() as client:90 r = client.get("https://google.com")91```9293```console94SSLKEYLOGFILE=test.log python test_script.py95cat test.log96# TLS secrets log file, generated by OpenSSL / Python97SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX98EXPORTER_SECRET XXXX99SERVER_TRAFFIC_SECRET_0 XXXX100CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX101CLIENT_TRAFFIC_SECRET_0 XXXX102SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX103EXPORTER_SECRET XXXX104SERVER_TRAFFIC_SECRET_0 XXXX105CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX106CLIENT_TRAFFIC_SECRET_0 XXXX107```108Use shebang-style headers in environment variables docs (#1260)Florimond Mancabefore adoption98bb548
line 109why, one step back109## `SSL_CERT_FILE`More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 110–112why, one step back110111Valid values: a filename112Fix doc capitalization (#1460)Daniel Saxtonbefore adoption9542a17
line 113why, one step back113If this environment variable is set then HTTPX will loadMore public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 114–122why, one step back114CA certificate from the specified file instead of the default115location.116117Example:118119```console120SSL_CERT_FILE=/path/to/ca-certs/ca-bundle.crt python -c "import httpx; httpx.get('https://example.com')"121```122Use shebang-style headers in environment variables docs (#1260)Florimond Mancabefore adoption98bb548
line 123why, one step back123## `SSL_CERT_DIR`More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 124why, one step back124Explain SSL_CERT_DIR specific format (#1470)Quentin Pradetbefore adoption645ae4e
line 125why, one step back125Valid values: a directory following an [OpenSSL specific layout](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_load_verify_locations.html).More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 126why, one step back126Explain SSL_CERT_DIR specific format (#1470)Quentin Pradetbefore adoption645ae4e
line 127why, one step back127If 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.More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 128–134why, one step back128129Example:130131```console132SSL_CERT_DIR=/path/to/ca-certs/ python -c "import httpx; httpx.get('https://example.com')"133```134Edited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
line 135why, one step back135## ProxiesMore public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 136why, one step back136Edited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
lines 137–141why, one step back137The environment variables documented below are used as a convention by various HTTP tooling, including:138139* [cURL](https://github.com/curl/curl/blob/master/docs/MANUAL.md#environment-variables)140* [requests](https://github.com/psf/requests/blob/master/docs/user/advanced.rst#proxies)141Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a
line 142why, one step back142For more information on using proxies in HTTPX, see [HTTP Proxying](advanced.md#http-proxying).Edited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
lines 143–145why, one step back143144### `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`145More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 146–147why, one step back146Valid values: A URL to a proxy147Edited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
line 148why, one step back148`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY` set the proxy to be used for `http`, `https`, or all requests respectively.More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 149–150why, one step back149150```bashEdited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
line 151why, one step back151export HTTP_PROXY=http://my-external-proxy.com:1234More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
lines 152–154why, one step back152153# This request will be sent through the proxy154python -c "import httpx; httpx.get('http://example.com')"Edited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
lines 155–158why, one step back155156# This request will be sent directly, as we set `trust_env=False`157python -c "import httpx; httpx.get('http://example.com', trust_env=False)"158More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 159why, one step back159```Revamp proxies documentation (#1123)Florimond Mancabefore adoption70cdd95
line 160why, one step back160Edited documentation about proxy-envs usage (#404) (#1257)cdelerbefore adoption78f2420
lines 161–177why, one step back161### `NO_PROXY`162163Valid values: a comma-separated list of hostnames/urls164165`NO_PROXY` disables the proxy for specific urls166167```bash168export HTTP_PROXY=http://my-external-proxy.com:1234169export NO_PROXY=http://127.0.0.1,python-httpx.org170171# As in the previous example, this request will be sent through the proxy172python -c "import httpx; httpx.get('http://example.com')"173174# These requests will be sent directly, bypassing the proxy175python -c "import httpx; httpx.get('http://127.0.0.1:5000/my-api')"176python -c "import httpx; httpx.get('https://www.python-httpx.org')"177```