Python 3 Script to Validate Domain Names and Get WHOIS information of Website Using python-whois Library Full Tutorial For Beginners

Python 3 Script to Validate Domain Names and Get WHOIS information of Website Using python-whois Library Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be validating domain names and getting the whois info of the website using python-whoisinfo library. All the full source code of application is shown below.

 

 

 

 

Get Started

 

 

 

In order to get started you need to install the python-whoisinfo library by issuing the pip command

 

pip install python-whoisinfo

 

After installing this library and just make an app.py file and copy paste the following code

 

app.py

 

 

Now we will be checking the validity of the domain in this block of code

 

 

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)
    
# list of registered & non registered domains to test our function
domains = [
    "freemediatools.com",
    "codingshiksha.com",
    "github.com",
    "unknownrandomdomain.com",
    "notregistered.co"
]
# iterate over domains
for domain in domains:
    print(domain, "is registered" if is_registered(domain) else "is not registered")

 

See also  Python 3 Tkinter Multiplayer Online Chess Board + (Computer AI) Modern Color Game GUI Desktop App Full Project For Beginners

 

 

Here in this python snippet code we are importing the python-whoisinfo library and then we are providing the list of domain names and then we are getting the domain validity i.e. if the domain is valid or not.

 

Now we can run this python script by executing the command as shown below

 

python app.py

 

 

 

 

Now we will be getting the whois information about the domain as shown below

 

 

import whois

# test with Google domain name
domain_name = "google.com"
whois_info = whois.whois(domain_name)
        # print the registrar
print("Domain registrar:", whois_info.registrar)
    
    # print the WHOIS server
print("WHOIS server:", whois_info.whois_server)
    
    # get the creation time
print("Domain creation date:", whois_info.creation_date)
    # get expiration date
print("Expiration date:", whois_info.expiration_date)

 

 

 

Leave a Reply