How to Get Domain Name Information in Python?

Posted in /  

How to Get Domain Name Information in Python?

Vinay Khatri
Last updated on June 15, 2024

    This tutorial helps you know how to get domain name information in Python. We will use the Python whois package to retrieve domain name information, such as the domain name, the domain credited on and registered by, the age of the domain, the expiry date, and the country code.

    But before that, let us briefly introduce the domain name and the whois package.

    So, let us explore below!

    What is a Domain Name?

    A domain name is a unique, human-readable, and easy-to-remember address of any web server that maps to its equivalent IP address . It helps clients locate and access a website easily on the internet. An IP address or Internet Protocol address is a unique numerical label, such as, assigned to each device in a network. It is generally difficult to remember. Hence, its equivalent domain name comes in handy.

    For instance, the domain name for our website is We can easily remember it and use it to access the website. So, when you visit, the domain name system (DNS) maps it to its equivalent IP address and enables you to access the website.

    Purchasing the domain name is the first step while creating any website . You need to consult a domain registration company, tell them about the domain name you want, check its availability, and get it if it is available. While doing so, the domain registration company requires you to provide several details, such as name, address, country, etc.

    Python whois Package

    To retrieve the domain name information, we will use the Python whois package , whose name is borrowed from the WHOIS query.

    WHOIS is an acronym for “Who is responsible for this domain name?”. It is the response protocol that queries databases storing information about registered Internet resources, such as domain names and IP addresses.

    The Python whois package provides an easy approach to retrieving domain name information. It is compatible with Python 2.4x and more and does not require any external dependency. The whois.whois() function takes the domain name as input and provides an object containing WHOIS attributes.

    Features of Python whois

    • The package can extract data for all top-level domains (TLDs), such as .com, .org, .net, etc.
    • It does not require any intermediary, such as a web service. The package parses the WHOIS data for the given domain by directly accessing the WHOIS server.
    • It can reformat the WHOIS data to make it more readable for us, which we refer to as WHOIS Data Normalization.
    • You get an automated testing suite.

    How to Get Domain Name Information in Python using the whois Package?

    Before writing and executing the program, have the python-whois library installed on your Python environment.

    Run the following Python pip install command on your terminal (Linux/Mac) or command prompt (Windows) to install the python-whois library.

    pip install python-whois

    Initially, we need to import the whois library into our Python program as follows:

    import whois 

    In addition, we will retrieve the domain name's expiry date. Hence, we need to import the datetime library.

    import datetime

    Next, we need to create a variable that stores the domain name entered by users.

    domain_name = input("Enter Domain Name: ")

    We have used the try-except block in our program. The try block lets you test the code for errors, and the except block handles the errors. So, if a user enters a valid domain name, the try block executes. Otherwise, the except block gets executed.

    Inside the try block, we will use the whois.whois() function. We have a variable holding the user-entered domain. We will pass this variable to the whois.whois() function, as follows:

    response = whois.whois(domain_name)

    The next step is to display all the domain name information using the print statement. Finally, add a statement as ‘No domain found’ for invalid user input in the except block.

    Python Program to Get Domain Name Information

    import whois
    import datetime
    domain_name = input("Enter Domain Name: ")
    #for valid domain
       #whois query 
       response= whois.whois(domain_name)
       print("The Domain’s Name:", response.domain_name)
       print("Domain Created on: ", response.creation_date.strftime('%d %B %Y'))
       print("Domain Registered By: ", response.registrar)
       age = ( - response.creation_date).days 
       print("The Age of the Domain is: ", age,"days")
       print("The Domain Expiry date is:", response.expiration_date.strftime('%d %B %Y'))
       print("The Domain Country code is:",
    #for invalid domain
       print(f"No domain found by {domain_name}")


    Enter Domain Name:
    The Domain’s Name: ['TECHGEEKBUZZ.COM', '']
    Domain Created on: 31 December 2018
    Domain Registered By:, LLC
    The Age of the Domain is: 758 days
    The Domain Expiry date is: 31 December 2022
    The Domain Country code is: IN


    This was all about retrieving domain name information in Python. We have used the python-whois package to fetch the domain data, which is public. Try the above program yourself and fetch information about various domain names within no time.

    If you like this article or have any issues implementing the above Python program, let us know by commenting below.

    People are also reading:

    Leave a Comment on this Post