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.
  · 3 min read · Updated jul 2022 · Web Scraping

Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

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 that has access to 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.

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 doesn'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.

Finally, if you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python. You can also check our resources and courses page to see the Python resources I recommend on various topics!

Learn alsoHow to Make a Subdomain Scanner in Python.

Happy Learning ♥

View Full Code
Sharing is caring!

Read Also

Comment panel