Last modified: Dec 08, 2023 By Alexander Williams

How To Use Whois API in Python

WHOIS (pronounced "who is") is a widely used tool for gathering information about domain names and websites. It functions as a public database where you can look up details such as:

  • Domain owner: This includes the name of the individual or organization registered to the domain name.
  • Contact information: This usually consists of the owner's email address, phone number, and sometimes, physical address.
  • Registrar: This is the company that manages the domain name registration on behalf of the owner.
  • Technical contact: This is the person responsible for the technical aspects of the website, such as setting up the DNS records.
  • Creation date and expiration date: This shows when the domain name was registered and when it will expire if not renewed.
  • Name servers: These are the servers that store information about the website's location on the internet.

In this article we'll learn how to use Whois API in python with real examples.

Step 1: Install the WHOIS Library

Start by installing the whois library using the following commands:

Using pip:

pip install python-whois

Using pip3:

pip install python-whois

Using conda:

conda install -c conda-forge python-whois

Note: The availability of the package in the conda repository may vary, and you might need to use pip or pip3 if the package is not available through conda.

How to use Whois API in Python

Now, let's create a Python script to fetch and display WHOIS information for a specified domain.

import whois

def get_whois_info(domain):
    try:
        # Fetch WHOIS information for the domain
        domain_info = whois.whois(domain)
        
        # Display the WHOIS information
        print("Domain: ", domain_info.domain)
        print("Registrar: ", domain_info.registrar)
        print("Creation Date: ", domain_info.creation_date)
        print("Expiration Date: ", domain_info.expiration_date)
        print("Name Servers: ", domain_info.name_servers)
        print("WHOIS Server: ", domain_info.whois_server)
        print("Updated Date: ", domain_info.updated_date)
        
    except whois.parser.PywhoisError as e:
        print(f"Error: {e}")

# Example usage
domain_name = "example.com"
get_whois_info(domain_name)

Here is the explanation of the code:

1. Import libraries:

  • The code imports the whois library, which allows you to fetch WHOIS information for a given domain name.

2. Define function:

  • The get_whois_info function takes a domain name as input and performs the following steps:
    • Tries to fetch WHOIS information for the domain using whois.whois(domain)
    • If successful, it extracts and displays various information like domain name, registrar, creation date, expiration date, name servers, WHOIS server, and updated date.
    • If an error occurs during the process, it catches the specific exception (whois.parser.PywhoisError) and prints the error message.

3. Example usage:

  • The code defines a variable domain_name with the value "example.com".
  • It then calls the get_whois_info function with this variable as input, which fetches and displays the WHOIS information for "example.com".

Overall, the code provides a simple example of how to use the whois library to access and display WHOIS information for a given domain name.

Now let's see how to fetch WHOIS Information for Multiple Domains:

import whois

def get_multiple_whois_info(domains):
    for domain in domains:
        print(f"\nWHOIS Information for {domain}:")
        get_whois_info(domain)

# Example usage with multiple domains
domain_names = ["example.com", "google.com", "github.com"]
get_multiple_whois_info(domain_names)

Explanation of the Python code example:

1. Import libraries:

  • Similar to the previous example, this code also imports the whois library.

2. Define function:

  • The get_multiple_whois_info function takes a list of domain names as input and performs the following steps:
    • Iterates through each domain name in the list.
    • Prints a message indicating the start of information for that specific domain.
    • Calls the get_whois_info function for each domain, effectively retrieving and displaying its WHOIS information.

3. Example usage with multiple domains:

  • The code defines a list domain_names containing three domain names: "example.com", "google.com", and "github.com".
  • It then calls the get_multiple_whois_info function with this list as input, which loops through each domain, calls the get_whois_info function for each, and displays the corresponding WHOIS information.

Overall, this code builds upon the previous example by demonstrating how to retrieve and display WHOIS information for multiple domains at once.

Whois Rate Limit

When working with the python-whois library or any WHOIS service, it's essential to be aware of rate limits imposed by WHOIS servers.

Rate limits are restrictions on the number of requests a user or IP address can make within a specific time frame to prevent abuse, ensure fair usage, and maintain server stability.

The python-whois library itself does not enforce rate limits, as it relies on external WHOIS servers for information. The rate limits are usually set by the WHOIS server that hosts the domain information.

Here are a few considerations regarding rate limits when using the python-whois library:

  1. WHOIS Server Policies:

    • Different WHOIS servers may have different rate-limiting policies. Some servers may be more permissive, while others might have stricter limits.
    • Review the documentation or terms of service of the specific WHOIS server you are querying to understand their rate-limiting policies.
  2. Respect Rate Limits:

    • Always respect the rate limits imposed by the WHOIS server. Exceeding these limits may result in temporary or permanent IP bans.
    • Adjust your code to handle rate limits gracefully. Implementing retry mechanisms with exponential backoff is a good practice.
  3. Consider Caching:

    • To minimize the number of requests made to the WHOIS server, consider implementing a caching mechanism. Cache the results of previous queries and refresh the cache only when necessary.
  4. Distributed Queries:

    • If you need to query WHOIS information for multiple domains, consider distributing the queries across multiple IP addresses or using different WHOIS servers to avoid hitting rate limits.
  5. User-Agent Headers:

    • Some WHOIS servers may use User-Agent headers to identify clients. Ensure that your application includes a proper User-Agent header in the requests. Follow the WHOIS server's guidelines for setting User-Agent information.

Here's a simple example demonstrating how to set a custom User-Agent header using the python-whois library:

import whois

def get_whois_info(domain):
    try:
        # Set a custom User-Agent header
        whois.whois.set_whois_lookup('whois.example.com')
        
        # Fetch WHOIS information for the domain
        domain_info = whois.whois(domain)
        
        # Display the WHOIS information
        print("Domain: ", domain_info.domain)
        # ... (other details)
        
    except whois.parser.PywhoisError as e:
        print(f"Error: {e}")

# Example usage
domain_name = "example.com"
get_whois_info(domain_name)

Always check the documentation of the specific WHOIS server you are querying for any guidelines on rate limits and usage policies.

Following best practices and being respectful of rate limits ensures a smooth and responsible interaction with WHOIS services.

Conclousion

This article has covered the basics of using the python-whois library to access and display WHOIS information for domain names. We've explored:

  1. Installation and basic usage: Installing the library and writing a script to fetch WHOIS information for a single domain.
  2. Multiple domains: Retrieving WHOIS information for multiple domains using a loop and the get_whois_info function.
  3. Rate limits: Understanding the importance of respecting rate limits imposed by WHOIS servers and implementing best practices like caching, distributed queries, and setting User-Agent headers.

By embracing responsible use and adhering to server policies, you can leverage the power of python-whois to effectively gather and analyze valuable domain information.