DomainExpiration/app/utils.py

17 lines
694 B
Python

import socket, ssl, datetime
def get_ssl_expiration(domain, port=443):
try:
ctx = ssl.create_default_context()
with socket.create_connection((domain, port), timeout=5) as sock:
with ctx.wrap_socket(sock, server_hostname=domain) as ssock:
cert = ssock.getpeercert()
notAfter = cert.get('notAfter')
if notAfter:
# Example certificate expiry format: "Jun 1 12:00:00 2025 GMT"
exp_date = datetime.datetime.strptime(notAfter, '%b %d %H:%M:%S %Y %Z')
return exp_date.strftime("%m/%d/%Y")
return "NO SSL"
except Exception:
return "NO SSL"