How to Get Domain Name Information in Python

Learn how to validate domain names using WHOIS, as well as getting domain name information such as domain registrar, creation date, expiration date and more in Python.
  · 4 min read · Updated jan 2023 · Ethical Hacking · Web Scraping

Want to code faster? Our Python Code Generator lets you create Python scripts with just a few clicks. Try it now!

A domain name is a string identifying a network domain; it represents an IP resource, such as a server hosting a website or just a computer accessing the Internet.

In simple terms, what we know as the domain name is the address of your website that people type in the browser URL to visit.

In this tutorial, we will use the whois library in Python to validate domain names and retrieve various domain information such as creation and expiration date, domain registrar, address and country of the owner, and more.

If you want to extract DNS info about a particular domain, then this tutorial is for you.

To get started, let's install the library:

pip3 install python-whois

WHOIS is a query and response protocol that is often used for querying databases that store registered domain names. It stores and delivers the content in a human-readable format. whois library simply queries a WHOIS server directly instead of going through an intermediate web service.

There is also a simple whois command in Linux to extract domain info, but since we're Python developers, then we'll be using Python for this.

Validating Domain Names

In this section, we'll use whois to tell whether a domain name exists and is registered, the below function does that:

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)

whois.whois() function raises an exception for domains that don't exist and may return without raising any exception even if the domain isn't registered, that is why we check whether domain_name exists, let's test this function:

# list of registered & non registered domains to test our function
domains = [
    "thepythoncode.com",
    "google.com",
    "github.com",
    "unknownrandomdomain.com",
    "notregistered.co"
]
# iterate over domains
for domain in domains:
    print(domain, "is registered" if is_registered(domain) else "is not registered")

We have defined some known domains and others that don't exist. Here is the output:

thepythoncode.com is registered
google.com is registered
github.com is registered
unknownrandomdomain.com is not registered
notregistered.co is not registered

Awesome, in the next section, we'll see how to get various useful information about domain names.

Getting Domain WHOIS Information

It is pretty straightforward to use this library, we just pass the domain name to the whois.whois() function:

import whois

# test with Google domain name
domain_name = "google.com"
if is_registered(domain_name):
    whois_info = whois.whois(domain_name)

Now to get the domain registrar (the company that manages the reservation of domain names), we simply access the attribute registrar:

    # print the registrar
    print("Domain registrar:", whois_info.registrar)

Getting the WHOIS server:

    # print the WHOIS server
    print("WHOIS server:", whois_info.whois_server)

Domain creation and expiration date:

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

Output:

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 

To see other various WHOIS information such as name servers, country, city, state, address, etc, just print whois_info:

    # print all other info
    print(whois_info)

Conclusion

There you have it! You just learned the easiest and quickest way to get domain name information in Python. Check python-whois Github repository.

Are you interested in learning more about ethical hacking and building tools to protect against cyber threats? Our Ethical Hacking with Python Ebook is the perfect resource for you! This comprehensive guide covers building tools in various topics, including information gathering, malware, and network packet manipulation. You'll learn how to build tools like a reverse shell, password cracking tools, and many more (More than 35 pentesting tools).

With step-by-step instructions and clear explanations, this EBook is perfect for both beginners and experienced professionals looking to expand their knowledge in the field of ethical hacking with Python. Don't miss out on this opportunity to become an expert in cyber security – get your copy today!

Learn alsoHow to Make a Subdomain Scanner in Python.

Happy Learning ♥

Why juggle between languages when you can convert? Check out our Code Converter. Try it out today!

View Full Code Improve My Code
Sharing is caring!



Read Also



Comment panel

    Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!