Last modified: Jan 10, 2023 By Alexander Williams

Python: check if domain's name is available

With the whois library, you can check if the domain's name is available.

whois Installation

pip install python-whois

How to use whois

Syntax:

whois.whois(domain)

If the domain is not available or registered, whois() returns domain info.

If the domain is available, return an error.

Example with a registered domain:


import whois

domain = 'pytutorial.com'

get_info = whois.whois(domain)

print(get_info)

Output:

{
  "domain_name": [
    "PYTUTORIAL.COM",
    "pytutorial.com"
  ],
  "registrar": "NAMECHEAP INC",
  "whois_server": "whois.namecheap.com",
  "referral_url": null,
  "updated_date": [
    "2020-12-23 14:56:23",
    "2020-11-25 04:11:43.170000"
  ],
  "creation_date": "2019-12-09 21:13:10",
  "expiration_date": "2021-12-09 21:13:10",
  "name_servers": [
    "CHINCHILLA.EZOICNS.COM",
    "CUTTLEFISH.EZOICNS.COM",
    "ORANG-UTAN.EZOICNS.COM",
    "TROPICBIRD.EZOICNS.COM",
    "chinchilla.ezoicns.com",
    "cuttlefish.ezoicns.com",
    "orang-utan.ezoicns.com",
    "tropicbird.ezoicns.com"
  ],
  "status": "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
  "emails": [
    "abuse@namecheap.com",
    "981a1f4faec6408f80bceab53de5ea98.protect@withheldforprivacy.com"
  ],
  "dnssec": "unsigned",
  "name": "Withheld for Privacy Purposes",
  "org": "Privacy service provided by Withheld for Privacy ehf",
  "address": "Kalkofnsvegur 2",
  "city": "Reykjavik",
  "state": "Capital Region",
  "zipcode": "101",
  "country": "IS"
}

Example with an available domain:


import whois

domain = 'pytutorialnoexist.com'

get_info = whois.whois(domain)

print(get_info)

Output:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    domain_info = whois.whois(domain)
  File "/home/py/.local/lib/python3.8/site-packages/whois/__init__.py", line 44, in whois

Check if a domain is available

As I said, the whois function returns an error when the domain is available.

So to handle this error, we need to use the try-except statement.

Example:


import whois

domain = 'pytutorialnoexist.com'

try:
    get_info = whois.whois(domain)
    print(f"{domain} is Registered")

except:
    print(f"{domain} is Available")

Output:

pytutorialnoexist.com is Available

Write our checker function

After understanding how the whois library works, let's now write our domain-checker function.

Code:


import whois

def is_domain_available(domain):
    try:
        get_info = whois.whois(domain)
        return False
    except:
        return True

Try it:


print(is_domain_available('pytutorialnoexist.com'))

Output:

True


print(is_domain_available('pytutorial.com'))

Output:

False