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.

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 has an 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 it.

In this tutorial, we will use whois library in Python to validate domain names and getting various domain information such as creation and expiration date, domain registrar 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 as a human readable format. whois library simply queries a WHOIS server directly instead of going through an intermediate web service.

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)

#python #developer

How to Get Domain Name Information in Python
6.10 GEEK