Skip to content

Commit 8dfc3ad

Browse files
Expand SSLContext documentation with additional examples (#7334)
1 parent cff007e commit 8dfc3ad

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

CHANGES/7334.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expanded SSL documentation with more examples (e.g. how to use certifi). -- by :user:`Dreamsorcerer`

docs/client_advanced.rst

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -480,26 +480,12 @@ checks can be relaxed by setting *ssl* to ``False``::
480480

481481
r = await session.get('https://example.com', ssl=False)
482482

483-
484483
If you need to setup custom ssl parameters (use own certification
485484
files for example) you can create a :class:`ssl.SSLContext` instance and
486-
pass it into the proper :class:`ClientSession` method::
487-
488-
sslcontext = ssl.create_default_context(
489-
cafile='/path/to/ca-bundle.crt')
490-
r = await session.get('https://example.com', ssl=sslcontext)
491-
492-
If you need to verify *self-signed* certificates, you can do the
493-
same thing as the previous example, but add another call to
494-
:meth:`ssl.SSLContext.load_cert_chain` with the key pair::
485+
pass it into the :meth:`ClientSession.request` methods or set it for the
486+
entire session with ``ClientSession(connector=TCPConnector(ssl=ssl_context))``.
495487

496-
sslcontext = ssl.create_default_context(
497-
cafile='/path/to/ca-bundle.crt')
498-
sslcontext.load_cert_chain('/path/to/client/public/device.pem',
499-
'/path/to/client/private/device.key')
500-
r = await session.get('https://example.com', ssl=sslcontext)
501-
502-
There is explicit errors when ssl verification fails
488+
There are explicit errors when ssl verification fails
503489

504490
:class:`aiohttp.ClientConnectorSSLError`::
505491

@@ -529,6 +515,34 @@ If you need to skip both ssl related errors
529515
except aiohttp.ClientSSLError as e:
530516
assert isinstance(e, ssl.CertificateError)
531517

518+
Example: Use certifi
519+
^^^^^^^^^^^^^^^^^^^^
520+
521+
By default, Python uses the system CA certificates. In rare cases, these may not be
522+
installed or Python is unable to find them, resulting in a error like
523+
`ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate`
524+
525+
One way to work around this problem is to use the `certifi` package::
526+
527+
ssl_context = ssl.create_default_context(cafile=certifi.where())
528+
async with ClientSession(connector=TCPConnector(ssl=ssl_context)) as sess:
529+
...
530+
531+
Example: Use self-signed certificate
532+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
533+
534+
If you need to verify *self-signed* certificates, you need to add a call to
535+
:meth:`ssl.SSLContext.load_cert_chain` with the key pair::
536+
537+
ssl_context = ssl.create_default_context()
538+
ssl_context.load_cert_chain("/path/to/client/public/device.pem",
539+
"/path/to/client/private/device.key")
540+
async with sess.get("https://example.com", ssl=ssl_context) as resp:
541+
...
542+
543+
Example: Verify certificate fingerprint
544+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
545+
532546
You may also verify certificates via *SHA256* fingerprint::
533547

534548
# Attempt to connect to https://www.python.org

0 commit comments

Comments
 (0)