I recently needed a script to inspect the SSL certificate expiry dates of several company websites so that we could replace certificates before they expired.
This post records one simple way to do that in Python.
An easy approach is to use the OpenSSL library to parse the certificate and read its expiry time:
1 | pip install pyOpenSSL |
Then the script can look like this:
1 | import ssl |
The returned expiry time looks like this:
1 | '20220219235959Z' |
That is a UTC timestamp string. If needed, you can convert it into a local timezone with another small helper:
1 | import datetime |
After that, it is straightforward to check whether a certificate is close to expiring.
I also came across a very useful site while testing this:
It provides many kinds of intentionally broken SSL scenarios. For example:
That makes it handy when testing how your code behaves around expired certificates and other SSL edge cases.