How To Use Python-Whois

In this tutorial, you are going to learn how to use the Python library python-whois for querying the Whois database.

This library simplifies the process of getting the contact information, registration details, and other important details of a domain.

With the knowledge of this library, you can easily extract the essential information of a web domain, which can be used for various purposes such as statistical analysis and domain monitoring.

Step 1: Install the python-whois library

The first thing you need to do is install the python-whois library. This package can be installed through pip. Open your terminal or command prompt and run the following command:

Step 2: Import the library and test a simple query

After the successful installation of the library, you can start using it to query the Whois server. Here is a simple example:

Now let’s analyze the output data:

domain_name: stackoverflow.com
registrar: Name.com, Inc.
...
creation_date: [2018-11-01 00:00:00, 2003-12-26 00:00:00]
expiration_date: [2023-11-01 00:00:00, 2021-12-26 00:00:00]
...
name_servers: ['NS-CLOUD-A1.GOOGLEDOMAINS.COM', 'NS-CLOUD-A2.GOOGLEDOMAINS.COM', ...]

As you can see, the response contains useful information such as the domain name, registration date, expiration date, and name servers.

Step 3: Accessing specific domain information

You can easily access a specific attribute of the whois response. For example, by using the below code, you can retrieve the expiration_date of the domain:

The output will display the expiration date:

Expiration Date: [2023-11-01 00:00:00, 2021-12-26 00:00:00]

Full code

Output

{
  "domain_name": [
    "STACKOVERFLOW.COM",
    "stackoverflow.com"
  ],
  "registrar": "CSC CORPORATE DOMAINS, INC.",
  "whois_server": "whois.corporatedomains.com",
  "referral_url": null,
  "updated_date": [
    "2022-08-17 04:32:10",
    "2022-08-17 00:32:11"
  ],
  "creation_date": [
    "2003-12-26 19:18:07",
    "2003-12-26 14:18:07"
  ],
  "expiration_date": "2024-02-02 11:59:59",
  "name_servers": [
    "NS-1033.AWSDNS-01.ORG",
    "NS-358.AWSDNS-44.COM",
    "NS-CLOUD-E1.GOOGLEDOMAINS.COM",
    "NS-CLOUD-E2.GOOGLEDOMAINS.COM",
    "ns-1033.awsdns-01.org",
    "ns-358.awsdns-44.com",
    "ns-cloud-e2.googledomains.com",
    "ns-cloud-e1.googledomains.com"
  ],
  "status": [
    "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
    "clientTransferProhibited http://www.icann.org/epp#clientTransferProhibited"
  ],
  "emails": [
    "[email protected]",
    "[email protected]"
  ],
  "dnssec": "unsigned",
  "name": "Domain Administrator",
  "org": "Stack Exchange, Inc.",
  "address": "110 William Street, 28th Floor",
  "city": "New York",
  "state": "NY",
  "registrant_postal_code": "10038",
  "country": "US"
}
Expiration Date: 2024-02-02 11:59:59

Conclusion

In this tutorial, you have learned how to use the python-whois library to query the Whois server for obtaining domain information. Using this library, you can easily get access to important information related to the domain such as registration and expiration date, name servers, and much more.