How to Check Whether Domain Name is Registered
CHECK WHETHER DOMAIN NAME IS REGISTERED OR NOT.
In our previous article, we learned about the WHOIS module in python, how to validate domain names using WHOIS, as well as getting domain name information such as domain registrar, creation date, expiration date, etc.
Through this article, we will use the WHOIS module of python to check whether the Domain name is registered or not. So I would suggest reading that article for a better understanding of this module.
In this discussion, we will not dig that part out again. Each line of code is written very clearly and the purpose of using that code is also mentioned within comments.
- Here we have imported the module “whois”.
- created a function “is_registered”: this function will simply return a boolean indicating whether the domain name is registered or not.
- To handle exception we have been using Exception Handling. After that, we have used “__name__” a built-in variable to run the module as the main program.
- Created a list and named it as “domains”.
- This list consists of some domain names.
- Then for loop is used to iterate over the list of domain names.
- And after validating it will return the output stating the so and so domain name is registered or not registered.
#importing whois package import whois def is_registered(domain_name): #this isthe function that will return 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) #driver code: if __name__ == "__main__": #list of registered or non registered domains to test our function. domains = [ "google.com", "github.com", "notregistered.co", "mk2345.in", "tutorialslink.com", "twitter.com", "facebook.com", "bhumika.in" ] #for loop to iterate over domains for domain in domains: print(domain, "is registered" if is_registered(domain) else "is not registered")
Output: the output of the above code is as shown below:
Liked our content? 😊 then don’t forget to share and comment.
Thank you for reading this article, in the upcoming article we will learn more python programming.