environment_variables.md
This file
Path
docs/environment_variables.md
131 lines · 4.5 KiB · Markdown
Last changed by
Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132Environment Variables=====================
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:
* On the client via `httpx.Client(trust_env=False)`* Per request via `client.get("<url>", trust_env=False)`
Here is a list of environment variables that HTTPX recognizesand what function they serve:
`HTTPX_DEBUG`-----------
Valid values: `1`, `true`
If this environment variable is set to a valid value then low-leveldetails about the execution of HTTP requests will be logged to `stderr`.
This can help you debug issues and see what's exactly being sentover the wire and to which location.
Example:
```python# test_script.py
import httpxclient = httpx.Client()client.get("https://google.com")```
```consoleuser@host:~$ HTTPX_DEBUG=1 python test_script.py20:54:17.585 - httpx.dispatch.connection_pool - acquire_connection origin=Origin(scheme='https' host='www.google.com' port=443)20:54:17.585 - httpx.dispatch.connection_pool - new_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))20:54:17.590 - httpx.dispatch.connection - start_connect host='www.google.com' port=443 timeout=TimeoutConfig(timeout=5.0)20:54:17.651 - httpx.dispatch.connection - connected http_version='HTTP/2'20:54:17.651 - httpx.dispatch.http2 - send_headers stream_id=1 headers=[(b':method', b'GET'), (b':authority', b'www.google.com'), ...]20:54:17.652 - httpx.dispatch.http2 - end_stream stream_id=120:54:17.681 - httpx.dispatch.http2 - receive_event stream_id=0 event=<RemoteSettingsChanged changed_settings:{...}>20:54:17.681 - httpx.dispatch.http2 - receive_event stream_id=0 event=<WindowUpdated stream_id:0, delta:983041>20:54:17.682 - httpx.dispatch.http2 - receive_event stream_id=0 event=<SettingsAcknowledged changed_settings:{}>20:54:17.739 - httpx.dispatch.http2 - receive_event stream_id=1 event=<ResponseReceived stream_id:1, headers:[(b':status', b'200'), ...]>20:54:17.741 - httpx.dispatch.http2 - receive_event stream_id=1 event=<DataReceived stream_id:1, flow_controlled_length:5224 data:>20:54:17.742 - httpx.dispatch.http2 - receive_event stream_id=1 event=<DataReceived stream_id:1, flow_controlled_length:59, data:>20:54:17.742 - httpx.dispatch.http2 - receive_event stream_id=1 event=<StreamEnded stream_id:1>20:54:17.742 - httpx.dispatch.http2 - receive_event stream_id=0 event=<PingReceived ping_data:0000000000000000>20:54:17.743 - httpx.dispatch.connection_pool - release_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))```
`SSLKEYLOGFILE`-----------
Valid values: a filename
If 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.
Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer.
Example:
```python# test_script.py
import httpxclient = httpx.Client()client.get("https://google.com")```
```consoleSSLKEYLOGFILE=test.log python test_script.pycat test.log# TLS secrets log file, generated by OpenSSL / PythonSERVER_HANDSHAKE_TRAFFIC_SECRET XXXXEXPORTER_SECRET XXXXSERVER_TRAFFIC_SECRET_0 XXXXCLIENT_HANDSHAKE_TRAFFIC_SECRET XXXXCLIENT_TRAFFIC_SECRET_0 XXXXSERVER_HANDSHAKE_TRAFFIC_SECRET XXXXEXPORTER_SECRET XXXXSERVER_TRAFFIC_SECRET_0 XXXXCLIENT_HANDSHAKE_TRAFFIC_SECRET XXXXCLIENT_TRAFFIC_SECRET_0 XXXX```
`SSL_CERT_FILE`-----------
Valid values: a filename
if this environment variable is set then HTTPX will loadCA certificate from the specified file instead of the defaultlocation.
Example:
```consoleSSL_CERT_FILE=/path/to/ca-certs/ca-bundle.crt python -c "import httpx; httpx.get('https://example.com')"```
`SSL_CERT_DIR`-----------
Valid values: a directory
if this environment variable is set then HTTPX will loadCA certificates from the specified location instead of the defaultlocation.
Example:
```consoleSSL_CERT_DIR=/path/to/ca-certs/ python -c "import httpx; httpx.get('https://example.com')"```
`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`----------------------------------------
Valid values: A URL to a proxy
Sets the proxy to be used for `http`, `https`, or all requests respectively.
```bashexport HTTP_PROXY=http://127.0.0.1:3080
# This request will be sent through the proxypython -c "import httpx; httpx.get('http://example.com')"```