How to Detect Expired Domains Using Python
Parse a list of domain registration data and compare expiry dates to today to find expired domains.
Python code
33 linesimport datetime
# List of test domains with fake registration and expiry dates
# Format: (domain, registration_date, expiry_date)
test_domains = [
('example.com', '2020-01-15', '2024-01-15'), # Expired
('google.com', '1997-09-15', '2026-09-15'), # Still active
('test-site.org', '2019-06-01', '2023-06-01'), # Expired
('anotherexample.net', '2022-03-20', '2025-03-20'), # Active
('old-domain.io', '2010-11-01', '2022-11-01'), # Expired
]
def check_domain_expiry(domain_list):
"""
Check which domains have expired by comparing their expiry date with today's date.
Returns list of expired domains.
"""
today = datetime.date.today()
expired_domains = []
for domain, reg_date_str, exp_date_str in domain_list:
expiry_date = datetime.datetime.strptime(exp_date_str, '%Y-%m-%d').date()
if expiry_date < today:
expired_domains.append((domain, str(expiry_date)))
return expired_domains
if __name__ == '__main__':
expired = check_domain_expiry(test_domains)
print("Expired Domains Found:")
for domain, expiry in expired:
print(f" {domain} - expired on {expiry}")
print(f"\nTotal expired: {len(expired)} out of {len(test_domains)} domains")
Output
Expired Domains Found:
example.com - expired on 2024-01-15
test-site.org - expired on 2023-06-01
old-domain.io - expired on 2022-11-01
Total expired: 3 out of 5 domains
How it works
The code uses datetime.datetime.strptime to convert date strings into date objects, then compares them with today's date using datetime.date.today(). Domains whose expiry date is earlier than today are considered expired. The results are stored as tuples of domain name and expiry date for clear output. This approach relies on the system clock, so ensure the server's date is accurate.
Common mistakes
- Assuming expiry dates are always in the same format without error handling
- Forgetting to convert date strings to date objects before comparison
- Using naive date comparisons that ignore time zones
Variations
- Use `datetime.datetime.now().date()` instead of `datetime.date.today()` if you need timezone awareness
- Use `pandas.read_csv` to load domains from a CSV file instead of using a hardcoded list
Real-world use cases
- Monitor a portfolio of owned domains to alert when renewals are due.
- Scan a list of client websites to identify expired domains that need attention.
- Automate batch checks for domain expiration in a registrar management tool.
Sponsored
More from Strings & text
Keep learning
Related tutorials and quizzes for this topic.