DomainExpiration/app/utils.py

30 lines
1.0 KiB
Python

import socket, ssl, datetime
import whois
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 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"
def get_whois_expiration(domain):
try:
w = whois.whois(domain)
exp_date = w.expiration_date
if isinstance(exp_date, list):
exp_date = exp_date[0]
if exp_date and hasattr(exp_date, "strftime"):
return exp_date.strftime("%m/%d/%Y")
return str(exp_date)
except Exception:
return "Error"