paint-brush
Python - Build a Domain Lookup Toolby@propy
8,599 reads
8,599 reads

Python - Build a Domain Lookup Tool

by ProPyNovember 14th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this tutorial, we will use the whois library to build a simple domain lookup tool. With this tool, we can retrieve domain name information such as the creation date, expiration date, address, country of the owner, and more. Whois is a query and response protocol that allows you to find the domain name info. In the following example, we'll get all information about hackernoon.com.com. The whois() method returns information about the given domain name.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Python - Build a Domain Lookup Tool
ProPy HackerNoon profile picture

In this tutorial, we will use the whois library to build a simple domain lookup tool. With this tool, we can retrieve domain name information such as the creation date, expiration date, address, country of the owner, and more.


If you are ready, let's get started.

WHOIS installation

Whois is a query and response protocol that allows you to find the domain name information. Whois has a python library named python-whois.


To install python-whois via pip, run the following command:

pip install python-whois # 👉️ Python 2

pip3 install python-whois # 👉️ Python 3

How to use WHOIS

It's pretty simple to use WHOIS. In the following example, we'll get all information about

hackernoon.com


import whois # 👉️ Import whois module

dm_info =  whois.whois("hackernoon.com") # 👉️ Get Domain Info

print(dm_info)


The whois() method returns information about the given domain name.


Output:

{
  "domain_name": [
    "HACKERNOON.COM",
    "hackernoon.com"
  ],
  "registrar": "GoDaddy.com, LLC",
  "whois_server": "whois.godaddy.com",
  "referral_url": null,
  "updated_date": [
    "2022-11-03 15:45:14",
    "2022-04-04 12:33:12"
  ],
  "creation_date": [
    "2016-04-03 23:11:48",
    "2016-04-03 18:11:48"
  ],
  "expiration_date": [
    "2023-04-03 23:11:48",
    "2023-04-03 18:11:48"
  ],
  "name_servers": [
    "AMBER.NS.CLOUDFLARE.COM",
    "GUY.NS.CLOUDFLARE.COM"
  ],
  "status": [
    "clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited",
    "clientRenewProhibited https://icann.org/epp#clientRenewProhibited",
    "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
    "clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited"
  ],
  "emails": "[email protected]",
  "dnssec": "unsigned",
  "name": "Registration Private",
  "org": "Domains By Proxy, LLC",
  "address": [
    "DomainsByProxy.com",
    "2155 E Warner Rd"
  ],
  "city": "Tempe",
  "state": "Arizona",
  "registrant_postal_code": "85284",
  "country": "US"
}


Now, let's see how to get specific information.

print("Registar:", dm_info.registrar) # 👉️ Get Registar

print("Creation Date:", dm_info.creation_date) # 👉️ Get Creation Date

print("Expiration Date:", dm_info.expiration_date) # 👉️ Expiration Date

print("Country:", dm_info.country) # 👉️ Get Country


Output:

Registar: GoDaddy.com, LLC
Creation Date: 2016-04-03 23:11:48
Expiration Date: 2023-04-03 23:11:48
Country: US


Note that! You will get the error if the domain name is not registered. For example:

dm_info = whois.whois("xysqsw.com") # 👉️ xysqsw.com  isn't registered


Output:

whois.parser.PywhoisError: No match for "XYSQSW.COM"


However, we need to use try and except to handle the error.

try:
    dm_info =  whois.whois("xysqsw.com") # 👉️ Get Domain Info
    print(dm_info)
except:
    print("Something Went Wrong")


Output:

Something Went Wrong


If you want to learn how to check the domain's availability, Check out  Check if the domain is available.

Build a domain lookup

After learning how to use the whois library, let's write a simple domain lookup function. This function will validate the given domain name and return the information of the domain.


First, we need to install the validators' packages.

pip install validators # 👉️ Python 2

pip3 install validators # 👉️ Python 2


Validators is a validation library used to validate email, date, IBAN, and domain...


Now, let's write the domain lookup function.

import validators # pip install validators

def domain_lookup(dm):

    if validators.domain(dm): # Check if Domain is Valid

        try:
            dm_info =  whois.whois(dm) # 👉️ Get Domain Info
            return dm_info

        except:
            return f"{dm} is not registered"

    else:
        return f"Enter a valid domain"


Test our function with a registered domain.

d_i = domain_lookup("facebook.com")

print(d_i)


Output:

{
  "domain_name": "FACEBOOK.COM",
  "registrar": "RegistrarSafe, LLC",
  "whois_server": "whois.registrarsafe.com",
  "referral_url": null,
  "updated_date": "2022-01-26 16:45:06",
  "creation_date": "1997-03-29 05:00:00",
  "expiration_date": "2031-03-30 04:00:00",
  "name_servers": [
    "A.NS.FACEBOOK.COM",
    "B.NS.FACEBOOK.COM",
    "C.NS.FACEBOOK.COM",
    "D.NS.FACEBOOK.COM"
  ],
  "status": [
    "clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited",
    "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
    "clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited",
    "serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited",
    "serverTransferProhibited https://icann.org/epp#serverTransferProhibited",
    "serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited",
    "serverUpdateProhibited https://www.icann.org/epp#serverUpdateProhibited",
    "clientDeleteProhibited https://www.icann.org/epp#clientDeleteProhibited",
    "clientTransferProhibited https://www.icann.org/epp#clientTransferProhibited",
    "serverDeleteProhibited https://www.icann.org/epp#serverDeleteProhibited",
    "serverTransferProhibited https://www.icann.org/epp#serverTransferProhibited",
    "clientUpdateProhibited https://www.icann.org/epp#clientUpdateProhibited"
  ],
  "emails": [
    "[email protected]",
    "[email protected]"
  ],
  "dnssec": "unsigned",
  "name": "Domain Admin",
  "org": "Meta Platforms, Inc.",
  "address": "1601 Willow Rd",
  "city": "Menlo Park",
  "state": "CA",
  "registrant_postal_code": "94025",
  "country": "US"
}


Unregistered domain:

d_i = domain_lookup("blabla123.com")

print(d_i)


Output:

blabla123.com is not registered


Invalid Domain:

d_i = domain_lookup("blabla123 h.com")

print(d_i)


Output:

Enter a valid domain

Conclusion

Excellent! You have successfully learned how to retrieve information from any domain using WHOIS. Feel free to leave comments if you have any questions or suggestions.


Happy Codding!