Skip to content

Python Cert Errors

MacOS

cd /Applications/Python\ 3.11/
./Install\ Certificates.command

AND/OR

pip install -U certifi

https://stackoverflow.com/questions/40684543/how-to-make-python-use-ca-certificates-from-mac-os-truststore

```python

Mostly derived from https://www.misterpki.com/python-get-ssl-certificate/

class ServerSSLCertificate:
   """
   Given a hostname (and optional port), connect to a remote server
   and fetch the server's SSL certificate. This does NOT validate
   the certificate, so can be used on iDRAC and other self-signed
   potentially invalid connections. The resulting certificate is
   loaded and available in PEM form, or as component data in the
   X509 object.
   """
   def init(self, server, port=443):
       if not OpenSSL:
           raise RuntimeError("ServerSSLCertificate requires OpenSSL module")
       connection = ssl.create_connection((server, port))
       context = ssl.SSLContext()
       sock = context.wrap_socket(connection, server_hostname=server)
       self.DER = sock.getpeercert(True)
       sock.close()
       self.PEM = ssl.DER_cert_to_PEM_cert(self.DER)
       self.X509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, self.PEM)
```