Check whether domain is registered

Installation

pip install pythonwhois

You might need to execute pip3 install pythonwhois --user or something similar.

Code

import pythonwhois


def is_registered(site):
    """Check if a domain has an WHOIS record."""
    details = pythonwhois.get_whois(site)
    return not details['raw'][0].startswith('No match for')


names = ['google', 'af35foobar90']
for name in names:
    site = '{}.com'.format(name)
    print('{}: {}'.format(site, is_registered(site)))

with pythonwhois if you favor, it could be

>>> import pythonwhois  # i'm using this http://cryto.net/pythonwhois
>>> domains = ['google.com', 'stackoverflow.com']
>>> for dom in domains:
...     details = pythonwhois.get_whois(dom)
...     print details['contacts']['registrant'] 

which returns a dictionary

{'city': u'Mountain View', 
'fax': u'+1.6506188571', 
'name': u'Dns Admin', 
'state': u'CA', 
'phone': u'+1.6502530000', 
'street': u'Please contact contact- [email protected], 1600 Amphitheatre Parkway', 
'country': u'US', 
'postalcode': u'94043', 
'organization': u'Google Inc.', 
'email': u'[email protected]'}

{'city': u'New York', 
 'name': u'Sysadmin Team', 
 'state': u'NY', 
 'phone': u'+1.2122328280', 
 'street': u'1 Exchange Plaza , Floor 26', 
 'country': u'US', 
 'postalcode': u'10006', 
 'organization': u'Stack Exchange, Inc.', 
 'email': u'[email protected]'}

edit: i checked your whois this code worked for me.

>>> import whois
>>> domains = ['google.com', 'stackoverflow.com']
>>> for dom in domains:
...     domain = whois.query(dom)
...     print domain.name, domain.registrar
... 
google.com MARKMONITOR INC.
stackoverflow.com NAME.COM, INC.

this api uses unix/linux's whois shell command and as it shown here you shouldn't add http:// before domain name. or if you have a unix/linux machine test this:

$ whois google.com

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information ...

but with http (it is maybe because of http(s) is a just a protocol type, and doesn't have any realiton with domain name itself)

$ whois http://google.com
No whois server is known for this kind of object.

I've had issues with python-whois in Python 3, but Python 2 works fine for me using the following code.

First, I would recommend uninstalling any whois module(s) you might have installed. Both python-whois (0.6.1) and whois (0.7) use the same import whois, which created some additional confusion for me.

Next, install python-whois through Command Prompt, Terminal, etc.

pip install python-whois

Once installed, enter the following code in your preferred python IDE.

"""
Python = 2.79
OS = Windows 10
IDE = PyCharm 4.5
PyPIPackage = python-whois 0.6.1
"""

import whois
url = 'example.com'
w = whois.whois(url)
print w

The result is a dictionary.

{
  "updated_date": "2015-08-14 00:00:00", 
  "status": [
    "clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited", 
    "clientTransferProhibited https://icann.org/epp#clientTransferProhibited", 
    "clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited"
  ], 
  "name": null, 
  "dnssec": null, 
  "city": null, 
  "expiration_date": "2016-08-13 00:00:00", 
...
...
...
  "address": null, 
  "name_servers": [
    "A.IANA-SERVERS.NET", 
    "B.IANA-SERVERS.NET"
  ], 
  "org": null, 
  "creation_date": "1995-08-14 00:00:00", 
  "emails": null
}

Tags:

Python

Dns