Skip to content
Myr

ssl.md

This file

Path

docs/advanced/ssl.md

89 lines · 3.5 KiB · Markdown

Last changed by

Documentation for SSL_CERT_FILE and SSL_CERT_DIR (#3579)Tobias Fischerbefore adoption652f051

History of this file

Take it

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 1–2why, one step back
1When 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

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 3why, one step back
3### Enabling and disabling verification

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 4why, one step back
4

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 5why, one step back
5By default httpx will verify HTTPS connections, and raise an error for invalid SSL cases...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 6why, one step back
6

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 7–10why, one step back
7```pycon
8>>> httpx.get("https://expired.badssl.com/")9httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)10```

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 11why, one step back
11

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 12why, one step back
12You can disable SSL verification completely and allow insecure requests...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 13why, one step back
13

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 14why, one step back
14```pycon

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 15why, one step back
15>>> httpx.get("https://expired.badssl.com/", verify=False)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 16–18why, one step back
16<Response [200 OK]>
17```18

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 19why, one step back
19### Configuring client instances

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 20why, one step back
20

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 21why, one step back
21If you're using a `Client()` instance you should pass any `verify=<...>` configuration when instantiating the client.

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 22why, one step back
22

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 23why, one step back
23By default the [certifi CA bundle](https://certifiio.readthedocs.io/en/latest/) is used for SSL verification.

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 24why, one step back
24

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 25why, one step back
25For more complex configurations you can pass an [SSL Context](https://docs.python.org/3/library/ssl.html) instance...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 26why, one step back
26

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

lines 27–30why, one step back
27```python
28import certifi29import httpx30import ssl

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 31why, one step back
31

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

lines 32–34why, one step back
32# This SSL context is equivelent to the default `verify=True`.
33ctx = ssl.create_default_context(cafile=certifi.where())34client = httpx.Client(verify=ctx)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 35–36why, one step back
35```
36

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 37why, one step back
37Using [the `truststore` package](https://truststore.readthedocs.io/) to support system certificate stores...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 38–39why, one step back
38
39```python

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 40–43why, one step back
40import ssl
41import truststore42import httpx43

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

lines 44–46why, one step back
44# Use system certificate stores.
45ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)46client = httpx.Client(verify=ctx)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 47–48why, one step back
47```
48

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 49why, one step back
49Loding an alternative certificate verification store using [the standard SSL context API](https://docs.python.org/3/library/ssl.html)...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 50–51why, one step back
50
51```python

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 52why, one step back
52import httpx

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 53why, one step back
53import ssl

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 54why, one step back
54

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

lines 55–57why, one step back
55# Use an explicitly configured certificate store.
56ctx = ssl.create_default_context(cafile="path/to/certs.pem") # Either cafile or capath.57client = httpx.Client(verify=ctx)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 58–59why, one step back
58```
59

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 60why, one step back
60### Client side certificates

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 61why, one step back
61

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 62why, one step back
62Client side certificates allow a remote server to verify the client. They tend to be used within private organizations to authenticate requests to remote servers.

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 63why, one step back
63

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 64why, one step back
64You can specify client-side certificates, using the [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain) API...

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 65why, one step back
65

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 66why, one step back
66```python

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

lines 67–69why, one step back
67ctx = ssl.create_default_context()
68ctx.load_cert_chain(certfile="path/to/client.pem") # Optionally also keyfile or password.69client = httpx.Client(verify=ctx)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 70–71why, one step back
70```
71

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 72why, one step back
72### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 73why, one step back
73

Documentation for SSL_CERT_FILE and SSL_CERT_DIR (#3579)Tobias Fischerbefore adoption652f051

line 74why, one step back
74`httpx` does respect the `SSL_CERT_FILE` and `SSL_CERT_DIR` environment variables by default. For details, refer to [the section on the environment variables page](../environment_variables.md#ssl_cert_file).

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

lines 75–77why, one step back
75
76### Making HTTPS requests to a local server77

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 78–79why, one step back
78When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.
79

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 80why, one step back
80If 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...

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

lines 81–82why, one step back
81
821. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file.

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

line 83why, one step back
832. 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.)

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

line 84why, one step back
843. Configure `httpx` to use the certificates stored in `client.pem`.

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 85why, one step back
85

Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0

lines 86–88why, one step back
86```python
87ctx = ssl.create_default_context(cafile="client.pem")88client = httpx.Client(verify=ctx)

Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c

line 89why, one step back
89```