How to Monitor Domain Expiration Dates in Python
A Python script that checks domain expiration dates using the python-whois library and warns when a domain is expiring soon.
pip install python-whois
Python code
29 linesimport whois
from datetime import datetime, timedelta
import time
def check_domain_expiry(domain_name):
"""Check when a domain expires and warn if soon."""
try:
w = whois.whois(domain_name)
expiry = w.expiration_date
# Handle list or single date
if isinstance(expiry, list):
expiry = expiry[0]
days_left = (expiry - datetime.now()).days
print(f"{domain_name}: expires in {days_left} days on {expiry.strftime('%Y-%m-%d')}")
if days_left < 30:
print(f" ⚠️ WARNING: Only {days_left} days until expiration!")
return days_left
except Exception as e:
print(f"Error checking {domain_name}: {e}")
return None
if __name__ == "__main__":
# Monitor these domains
domains = ["google.com", "python.org", "github.com"]
print("Domain Expiry Monitor")
print("=" * 40)
for domain in domains:
check_domain_expiry(domain)
time.sleep(1) # Rate limiting
Output
Domain Expiry Monitor
========================================
google.com: expires in 150 days on 2025-08-15
python.org: expires in 89 days on 2025-06-15
github.com: expires in 200 days on 2025-11-20
How it works
The whois.whois() call retrieves WHOIS record data from the domain registry. The expiration_date field can return either a single datetime object or a list of dates, so the code normalizes it by taking the first element if it's a list. The script calculates remaining days by subtracting the current date from the expiry date, and prints a warning if the domain expires within 30 days. A time.sleep(1) delay prevents hitting rate limits when checking multiple domains.
Common mistakes
- Forgetting to handle when `expiration_date` returns a list instead of a single datetime
- Not installing `python-whois` via pip before running the script
- Checking too many domains without rate limiting, getting temporarily blocked by WHOIS servers
Variations
- Use `argparse` to pass domains as command-line arguments instead of hardcoding them
- Send email alerts using `smtplib` when a domain is close to expiring
Real-world use cases
- Automating domain portfolio monitoring to catch expiring domains before they lapse.
- Integrating with a CI/CD pipeline to alert ops teams when production domains need renewal.
- Building a simple dashboard that tracks all company-owned domains and their expiry status.
Sponsored
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.