Python: Check TLS certificates expiration date
Nov 5, 2022
To check the SSL certificate expiration date, we will use the OpenSSL library. This package provides a high-level interface to the functions in the OpenSSL library.
Following is the code snippet to use. You can execute it in jupyter notebook or create a .py file to execute it.
from datetime import datetime
import OpenSSL
import ssl
cert=ssl.get_server_certificate(('www.yahoo.com', 443))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
bytes=x509.get_notAfter()
print(bytes)
timestamp = bytes.decode('utf-8')
print (datetime.strptime(timestamp, '%Y%m%d%H%M%S%z').date().isoformat())
Output:
b'20230104235959Z'
2023-01-04
We can compare it by checking manually.