You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you're using a `Client()` instance you should pass any SSL context when instantiating the client.
35
-
36
-
```pycon
37
-
>>> context = httpx.SSLContext()
38
-
>>> client = httpx.Client(ssl_context=context)
39
-
```
40
-
41
-
The `client.get(...)` method and other request methods on a `Client` instance *do not* support changing the SSL settings on a per-request basis.
42
-
43
-
If you need different SSL settings in different cases you should use more than 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.
44
-
45
-
### Configuring certificate stores
46
-
47
-
By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/).
48
-
49
-
You can load additional certificate verification using the [`.load_verify_locations()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations) API:
If you're using a `Client()` instance you should pass any `verify=<...>` configuration when instantiating the client.
68
22
69
-
### Client side certificates
23
+
By default the [certifi CA bundle](https://certifiio.readthedocs.io/en/latest/) is used for SSL verification.
70
24
71
-
You can also specify a local cert to use as a client-side certificate, using the [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain) API:
25
+
For more complex configurations you can pass an [SSL Context](https://docs.python.org/3/library/ssl.html) instance...
Or working [directly with Python's standard library](https://docs.python.org/3/library/ssl.html)...
49
+
Loding an alternative certificate verification store using [the standard SSL context API](https://docs.python.org/3/library/ssl.html)...
122
50
123
51
```python
124
-
import ssl
125
52
import httpx
53
+
import ssl
126
54
127
-
ssl_context = ssl.create_default_context()
128
-
client = httpx.Client(ssl_context=ssl_context)
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)
129
58
```
130
59
131
-
### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`
60
+
### Client side certificates
132
61
133
-
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.
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.
134
63
135
-
For example...
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...
136
65
137
66
```python
138
-
context = httpx.SSLContext()
139
-
140
-
# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured.
141
-
if os.environ.get("SSL_CERT_FILE") or os.environ.get("SSL_CERT_DIR"):
142
-
context.load_verify_locations(
143
-
cafile=os.environ.get("SSL_CERT_FILE"),
144
-
capath=os.environ.get("SSL_CERT_DIR"),
145
-
)
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)
146
70
```
147
71
148
-
## `SSLKEYLOGFILE`
149
-
150
-
Valid values: a filename
151
-
152
-
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.
72
+
### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`
153
73
154
-
Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer.
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.
155
75
156
-
Example:
76
+
For example...
157
77
158
78
```python
159
-
# test_script.py
160
-
import httpx
161
-
162
-
with httpx.Client() as client:
163
-
r = client.get("https://google.com")
164
-
```
165
-
166
-
```console
167
-
SSLKEYLOGFILE=test.log python test_script.py
168
-
cat test.log
169
-
# TLS secrets log file, generated by OpenSSL / Python
170
-
SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX
171
-
EXPORTER_SECRET XXXX
172
-
SERVER_TRAFFIC_SECRET_0 XXXX
173
-
CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX
174
-
CLIENT_TRAFFIC_SECRET_0 XXXX
175
-
SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX
176
-
EXPORTER_SECRET XXXX
177
-
SERVER_TRAFFIC_SECRET_0 XXXX
178
-
CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX
179
-
CLIENT_TRAFFIC_SECRET_0 XXXX
79
+
# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured.
When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.
185
91
186
-
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:
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...
187
93
188
94
1. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file.
189
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.)
190
-
3.Tell HTTPX to use the certificates stored in `client.pem`:
96
+
3.Configure `httpx` to use the certificates stored in `client.pem`.
0 commit comments