ssl.md
This file
Path
docs/advanced/ssl.md
89 lines · 3.5 KiB · Markdown
Last changed by
docs/ssl: fix typo (#3703)Riccardo Magliocchettibefore adoptionca097c9
History of this fileTake it
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 1–2why, one step back1When 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).2Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 3why, one step back3### Enabling and disabling verificationDocs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 4why, one step back4Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 5why, one step back5By default httpx will verify HTTPS connections, and raise an error for invalid SSL cases...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 6why, one step back6Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 7–10why, one step back7```pycon8>>> 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 back11Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 12why, one step back12You can disable SSL verification completely and allow insecure requests...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 13why, one step back13Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 14why, one step back14```pyconGraceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 15why, one step back15>>> httpx.get("https://expired.badssl.com/", verify=False)Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 16–18why, one step back16<Response [200 OK]>17```18Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 19why, one step back19### Configuring client instancesDocs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 20why, one step back20Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 21why, one step back21If 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 back22Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 23why, one step back23By 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 back24Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 25why, one step back25For 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 back26Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
lines 27–30why, one step back27```python28import certifi29import httpx30import sslIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 31why, one step back31docs/ssl: fix typo (#3703)Riccardo Magliocchettibefore adoptionca097c9
line 32why, one step back32# This SSL context is equivalent to the default `verify=True`.Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
lines 33–34why, one step back33ctx = ssl.create_default_context(cafile=certifi.where())34client = httpx.Client(verify=ctx)Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 35–36why, one step back35```36Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 37why, one step back37Using [the `truststore` package](https://truststore.readthedocs.io/) to support system certificate stores...Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 38–39why, one step back3839```pythonIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 40–43why, one step back40import ssl41import truststore42import httpx43Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
lines 44–46why, one step back44# 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 back47```48Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 49why, one step back49Loding 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 back5051```pythonIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 52why, one step back52import httpxGraceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 53why, one step back53import sslIntroduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
line 54why, one step back54Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
lines 55–57why, one step back55# 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 back58```59Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 60why, one step back60### Client side certificatesDocs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 61why, one step back61Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 62why, one step back62Client 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 back63Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 64why, one step back64You 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 back65Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 66why, one step back66```pythonGraceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
lines 67–69why, one step back67ctx = 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 back70```71Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 72why, one step back72### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 73why, one step back73Documentation for SSL_CERT_FILE and SSL_CERT_DIR (#3579)Tobias Fischerbefore adoption652f051
line 74why, one step back74`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 back7576### Making HTTPS requests to a local server77Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 78–79why, one step back78When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.79Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
line 80why, one step back80If 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 back81821. 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 back832. 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 back843. Configure `httpx` to use the certificates stored in `client.pem`.Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 85why, one step back85Graceful upgrade path for 0.28. (#3394)Tom Christiebefore adoption1805ee0
lines 86–88why, one step back86```python87ctx = ssl.create_default_context(cafile="client.pem")88client = httpx.Client(verify=ctx)Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
line 89why, one step back89```