| 1 |
When making a request over HTTPS, HTTPX needs to verify the identity of the requested host. To do this, it uses a bundle of SSL certificates (a.k.a. CA bundle) delivered by a trusted certificate authority (CA). |
| 2 |
|
| 3 |
### Enabling and disabling verification |
| 4 |
|
| 5 |
By default httpx will verify HTTPS connections, and raise an error for invalid SSL cases... |
| 6 |
|
| 7 |
```pycon |
| 8 |
>>> httpx.get("https://expired.badssl.com/") |
| 9 |
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997) |
| 10 |
``` |
| 11 |
|
| 12 |
You can disable SSL verification completely and allow insecure requests... |
| 13 |
|
| 14 |
```pycon |
| 15 |
>>> httpx.get("https://expired.badssl.com/", verify=False) |
| 16 |
<Response [200 OK]> |
| 17 |
``` |
| 18 |
|
| 19 |
### Configuring client instances |
| 20 |
|
| 21 |
If you're using a `Client()` instance you should pass any `verify=<...>` configuration when instantiating the client. |
| 22 |
|
| 23 |
By default the [certifi CA bundle](https://certifiio.readthedocs.io/en/latest/) is used for SSL verification. |
| 24 |
|
| 25 |
For more complex configurations you can pass an [SSL Context](https://docs.python.org/3/library/ssl.html) instance... |
| 26 |
|
| 27 |
```python |
| 28 |
import certifi |
| 29 |
import httpx |
| 30 |
import ssl |
| 31 |
|
| 32 |
# This SSL context is equivelent to the default `verify=True`. |
| 33 |
ctx = ssl.create_default_context(cafile=certifi.where()) |
| 34 |
client = httpx.Client(verify=ctx) |
| 35 |
``` |
| 36 |
|
| 37 |
Using [the `truststore` package](https://truststore.readthedocs.io/) to support system certificate stores... |
| 38 |
|
| 39 |
```python |
| 40 |
import ssl |
| 41 |
import truststore |
| 42 |
import httpx |
| 43 |
|
| 44 |
# Use system certificate stores. |
| 45 |
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 46 |
client = httpx.Client(verify=ctx) |
| 47 |
``` |
| 48 |
|
| 49 |
Loding an alternative certificate verification store using [the standard SSL context API](https://docs.python.org/3/library/ssl.html)... |
| 50 |
|
| 51 |
```python |
| 52 |
import httpx |
| 53 |
import ssl |
| 54 |
|
| 55 |
# Use an explicitly configured certificate store. |
| 56 |
ctx = ssl.create_default_context(cafile="path/to/certs.pem") # Either cafile or capath. |
| 57 |
client = httpx.Client(verify=ctx) |
| 58 |
``` |
| 59 |
|
| 60 |
### Client side certificates |
| 61 |
|
| 62 |
Client side certificates allow a remote server to verify the client. They tend to be used within private organizations to authenticate requests to remote servers. |
| 63 |
|
| 64 |
You can specify client-side certificates, using the [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain) API... |
| 65 |
|
| 66 |
```python |
| 67 |
ctx = ssl.create_default_context() |
| 68 |
ctx.load_cert_chain(certfile="path/to/client.pem") # Optionally also keyfile or password. |
| 69 |
client = httpx.Client(verify=ctx) |
| 70 |
``` |
| 71 |
|
| 72 |
### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR` |
| 73 |
|
| 74 |
Unlike `requests`, the `httpx` package does not automatically pull in [the environment variables `SSL_CERT_FILE` or `SSL_CERT_DIR`](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_default_verify_paths.html). If you want to use these they need to be enabled explicitly. |
| 75 |
|
| 76 |
For example... |
| 77 |
|
| 78 |
```python |
| 79 |
# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured. |
| 80 |
# Otherwise default to certifi. |
| 81 |
ctx = ssl.create_default_context( |
| 82 |
cafile=os.environ.get("SSL_CERT_FILE", certifi.where()), |
| 83 |
capath=os.environ.get("SSL_CERT_DIR"), |
| 84 |
) |
| 85 |
client = httpx.Client(verify=ctx) |
| 86 |
``` |
| 87 |
|
| 88 |
### Making HTTPS requests to a local server |
| 89 |
|
| 90 |
When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections. |
| 91 |
|
| 92 |
If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it... |
| 93 |
|
| 94 |
1. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file. |
| 95 |
2. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.) |
| 96 |
3. Configure `httpx` to use the certificates stored in `client.pem`. |
| 97 |
|
| 98 |
```python |
| 99 |
ctx = ssl.create_default_context(cafile="client.pem") |
| 100 |
client = httpx.Client(verify=ctx) |
| 101 |
``` |