Domain Name Notifications with Python

July 06, 2012

I wanted to write a small program to automatically alert me if certain domain names became available to register. My first thought was to write a Bash script to run the whois command and parse the output with a combination of Grep and Sed, but after looking at the mess that whois returns, I opted to go another route.

I found a Python module to retrieve WHOIS data and installed it with pip install whois. I played around it for a few minutes and realized that while the website mentions support for .net addresses, the .net domain wasn’t actually coded into the module. Luckily, the data returned by whois is in the same format for both .net and .com domain names. I added the following few lines to /usr/lib/python3.2/site-packages/whois/ and it started to work fine.

net = {
'extend' : 'com'

I wanted to have the script only output something when a domain name was close to being expired so that it could be easily installed into a cron job. The final script is here:

#!/usr/bin/env python

Author: Tim Montague
Description: Checks expiration dates of domain names

import argparse
import whois
import sys
import datetime

def main():
	parser = argparse.ArgumentParser(
		description="Outputs remaining days until domain name expires if the\
		number of days is less then a specified amount (defaults to 7 days)")
	parser.add_argument("domainnames", nargs='+')
	parser.add_argument("-v", "--verbose", action="store_true",
			help="print days remaining even if less than threshold")
	parser.add_argument("-d", "--days", help="set days remaining threshold",
			type=int, default=7)
	args = parser.parse_args()

	for d in args.domainnames:
			w = whois.query(d, ignore_returncode=1)
			if w:
				days = (w.expiration_date - datetime.datetime.utcnow()).days
				if args.verbose or days < args.days:
					print (d, "expires in", days, "days!")
				print("Domain not found: ", d, file=sys.stderr)
		except Exception as e:
			print(e, file=sys.stderr)
if __name__ == '__main__':

This script works perfectly for me. I set up cron to run it daily, so whenever one of the domains I’m watching comes close to being available, I’ll get an email notification.