ssl.md
This file
Path
docs/advanced/ssl.md
100 lines · 3.7 KiB · Markdown
Last changed by
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
History of this fileTake it
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 1–100why, 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).23## Changing the verification defaults45By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/). This is what you want in most cases, even though some advanced situations may require you to use a different set of certificates.67If you'd like to use a custom CA bundle, you can use the `verify` parameter.89```python10import httpx1112r = httpx.get("https://example.org", verify="path/to/client.pem")13```1415Alternatively, you can pass a standard library `ssl.SSLContext`.1617```pycon18>>> import ssl19>>> import httpx20>>> context = ssl.create_default_context()21>>> context.load_verify_locations(cafile="/tmp/client.pem")22>>> httpx.get('https://example.org', verify=context)23<Response [200 OK]>24```2526We also include a helper function for creating properly configured `SSLContext` instances.2728```pycon29>>> context = httpx.create_ssl_context()30```3132The `create_ssl_context` function accepts the same set of SSL configuration arguments33(`trust_env`, `verify`, `cert` and `http2` arguments)34as `httpx.Client` or `httpx.AsyncClient`3536```pycon37>>> import httpx38>>> context = httpx.create_ssl_context(verify="/tmp/client.pem")39>>> httpx.get('https://example.org', verify=context)40<Response [200 OK]>41```4243Or you can also disable the SSL verification entirely, which is _not_ recommended.4445```python46import httpx4748r = httpx.get("https://example.org", verify=False)49```5051## SSL configuration on client instances5253If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client.5455```python56client = httpx.Client(verify=False)57```5859The `client.get(...)` method and other request methods *do not* support changing the SSL settings on a per-request basis. If you need different SSL settings in different cases you should use more that one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool.6061## Client Side Certificates6263You can also specify a local cert to use as a client-side certificate, either a path to an SSL certificate file, or two-tuple of (certificate file, key file), or a three-tuple of (certificate file, key file, password)6465```python66cert = "path/to/client.pem"67client = httpx.Client(cert=cert)68response = client.get("https://example.org")69```7071Alternatively...7273```python74cert = ("path/to/client.pem", "path/to/client.key")75client = httpx.Client(cert=cert)76response = client.get("https://example.org")77```7879Or...8081```python82cert = ("path/to/client.pem", "path/to/client.key", "password")83client = httpx.Client(cert=cert)84response = client.get("https://example.org")85```8687## Making HTTPS requests to a local server8889When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.9091If 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:92931. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file.941. 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.)951. Tell HTTPX to use the certificates stored in `client.pem`:9697```python98client = httpx.Client(verify="/tmp/client.pem")99response = client.get("https://localhost:8000")100```