snipplets.dev/code/Python/ssl_expiry_datetime.py

51 lines
1.1 KiB
Python
Raw Normal View History

2024-03-20 00:23:02 +03:00
"""
https://stackoverflow.com/questions/41620369/how-to-get-ssl-certificate-details-using-python
"""
import socket
import ssl
import datetime
domains = [
'iiiypuk.me',
'a2s.su',
'ya.ru'
]
def ssl_expiry_datetime(hostname):
ssl_dateformat = r'%b %d %H:%M:%S %Y %Z'
context = ssl.create_default_context()
context.check_hostname = False
conn = context.wrap_socket(
socket.socket(socket.AF_INET),
server_hostname=hostname,
)
# 5 second timeout
conn.settimeout(5.0)
conn.connect((hostname, 443))
ssl_info = conn.getpeercert()
# Python datetime object
return datetime.datetime.strptime(ssl_info["notAfter"], ssl_dateformat)
if __name__ == "__main__":
for value in domains:
now = datetime.datetime.now()
try:
expire = ssl_expiry_datetime(value)
diff = expire - now
print(
"Domain name: {} Expiry Date: {} Expiry Day: {}".format(
value, expire.strftime("%Y-%m-%d"), diff.days
)
)
except Exception as e:
print(e)