Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

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.

Easy Python 3.9+ Jun 28, 2026 Automation & scripting 2 views 0 copies

Requires third-party packages — install first
pip install python-whois

Python code

29 lines
Python 3.9+
import 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

stdout
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

  1. Use `argparse` to pass domains as command-line arguments instead of hardcoding them
  2. 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

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.