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

Before we get started, have you tried our new Python Code Assistant? It's like having an expert coder at your fingertips. Check it out!

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
        w = whois.whois(domain_name)
    except Exception:
        return False
        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 = [
# 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: is registered is registered is registered is not registered 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 = ""
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)


Domain registrar: MarkMonitor Inc.
WHOIS server:        
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


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 ♥

Save time and energy with our Python Code Generator. Why start from scratch when you can generate? Give it a try!

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!