@@ -1873,13 +1873,15 @@ to speed up repeated connections from the same clients.
1873
1873
:meth: `~SSLContext.wrap_socket ` in order to match the hostname. Enabling
1874
1874
hostname checking automatically sets :attr: `~SSLContext.verify_mode ` from
1875
1875
:data: `CERT_NONE ` to :data: `CERT_REQUIRED `. It cannot be set back to
1876
- :data: `CERT_NONE ` as long as hostname checking is enabled.
1876
+ :data: `CERT_NONE ` as long as hostname checking is enabled. The
1877
+ :data: `PROTOCOL_TLS_CLIENT ` protocol enables hostname checking by default.
1878
+ With other protocols, hostname checking must be enabled explicitly.
1877
1879
1878
1880
Example::
1879
1881
1880
1882
import socket, ssl
1881
1883
1882
- context = ssl.SSLContext()
1884
+ context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2 )
1883
1885
context.verify_mode = ssl.CERT_REQUIRED
1884
1886
context.check_hostname = True
1885
1887
context.load_default_certs()
@@ -2181,19 +2183,23 @@ If you prefer to tune security settings yourself, you might create
2181
2183
a context from scratch (but beware that you might not get the settings
2182
2184
right)::
2183
2185
2184
- >>> context = ssl.SSLContext()
2185
- >>> context.verify_mode = ssl.CERT_REQUIRED
2186
- >>> context.check_hostname = True
2186
+ >>> context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
2187
2187
>>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
2188
2188
2189
2189
(this snippet assumes your operating system places a bundle of all CA
2190
2190
certificates in ``/etc/ssl/certs/ca-bundle.crt ``; if not, you'll get an
2191
2191
error and have to adjust the location)
2192
2192
2193
+ The :data: `PROTOCOL_TLS_CLIENT ` protocol configures the context for cert
2194
+ validation and hostname verification. :attr: `~SSLContext.verify_mode ` is
2195
+ set to :data: `CERT_REQUIRED ` and :attr: `~SSLContext.check_hostname ` is set
2196
+ to ``True ``. All other protocols create SSL contexts with insecure defaults.
2197
+
2193
2198
When you use the context to connect to a server, :const: `CERT_REQUIRED `
2194
- validates the server certificate: it ensures that the server certificate
2195
- was signed with one of the CA certificates, and checks the signature for
2196
- correctness::
2199
+ and :attr: `~SSLContext.check_hostname ` validate the server certificate: it
2200
+ ensures that the server certificate was signed with one of the CA
2201
+ certificates, checks the signature for correctness, and verifies other
2202
+ properties like validity and identity of the hostname::
2197
2203
2198
2204
>>> conn = context.wrap_socket(socket.socket(socket.AF_INET),
2199
2205
... server_hostname="www.python.org")
0 commit comments