Python Example | Get Domain Name Information

This python example code shows us how to get domain name information using python.

Step 1. Install python-whois

pip3 install python-whois

Step 2: Check domain is registered

import whois # pip install python-whois

def is_registered(domain_name):
    """
    A function that returns a boolean indicating 
    whether a `domain_name` is registered
    """
    try:
        w = whois.whois(domain_name)
    except Exception:
        return False
    else:
        return bool(w.domain_name)

Step 3: get domain information

domain_name = "google.com"
if is_registered(domain_name):
    whois_info = whois.whois(domain_name)

Step 4: output information

# print the registrar
print("Domain registrar:", whois_info.registrar)
# print the WHOIS server
print("WHOIS server:", whois_info.whois_server)
# get the creation time
print("Domain creation date:", whois_info.creation_date)
# get expiration date
print("Expiration date:", whois_info.expiration_date)

Run this code, you will get:

Domain registrar: MarkMonitor Inc.
WHOIS server: whois.markmonitor.com        
Domain creation date: 1997-09-15 04:00:00  
Expiration date: 2028-09-14 04:00:00