A domain name is a string identifying a network domain, it represents an IP resource, such as a server hosting a website, or just a computer that has an access to the Internet.
In simple terms, what we know as the domain name is the address of your website that people type in the browser URL to visit it.
In this tutorial, we will use whois library in Python to validate domain names and retrieve various domain information such as creation and expiration date, domain registrar, address and country of the owner and more.
To get started, let's install the library:
pip3 install python-whois
WHOIS is a query and response protocol that is often used for querying databases that store registered domain names. It stores and delivers the content as a human readable format. whois library simply queries a WHOIS server directly instead of going through an intermediate web service.
In this section, we'll use whois to tell whether a domain name exists and is registered, the below function does that:
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)
whois.whois() function raises an exception for domains that doesn't exist, and may return without raising any exception even if the domain isn't registered, that is why we check whether
domain_name exists, let's test this function:
# list of registered & non registered domains to test our function domains = [ "thepythoncode.com", "google.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")
We have defined some known domains and others that doesn't exist, here is the output:
thepythoncode.com is registered google.com is registered github.com is registered unknownrandomdomain.com is not registered notregistered.co is not registered
Awesome, in the next section, we'll see how to get various useful information about domain names.
It is pretty straightforward to use this library, we just pass the domain name to the
import whois # test with Google domain name domain_name = "google.com" if is_registered(domain_name): whois_info = whois.whois(domain_name)
Now to get the domain registrar (company that manages the reservation of domain names), we simply access the attribute registrar:
# print the registrar print("Domain registrar:", whois_info.registrar)
Getting the WHOIS server:
# print the WHOIS server print("WHOIS server:", whois_info.whois_server)
Domain creation and expiration date:
# get the creation time print("Domain creation date:", whois_info.creation_date) # get expiration date print("Expiration date:", whois_info.expiration_date)
Domain registrar: MarkMonitor Inc. WHOIS server: whois.markmonitor.com Domain creation date: 1997-09-15 04:00:00 Expiration date: 2028-09-14 04:00:00
To see other various WHOIS information such as name servers, country, city, state, address, etc, just print
# print all other info print(whois_info)
There you have it! You just learned the easiest and quickest way to get domain name information in Python. Check python-whois Github repository.
Learn also: How to Make a Subdomain Scanner in Python.
Happy Learning ♥View Full Code