Python Whois Examples

This article will tell you how to use the python-whois module to get the whois information of a website domain with examples.

1. Install The python-whois Module.

  1. Open a terminal and run the command pip install python-whois to install the python-whois module.
    > pip install python-whois
    Defaulting to user installation because normal site-packages is not writeable
    Collecting python-whois
      Downloading python-whois-0.8.0.tar.gz (109 kB)
         ---------------------------------------- 109.6/109.6 kB 107.8 kB/s eta 0:00:00
      Preparing metadata (setup.py) ... done
    Collecting future
      Downloading future-0.18.2.tar.gz (829 kB)
         ---------------------------------------- 829.2/829.2 kB 208.0 kB/s eta 0:00:00
      Preparing metadata (setup.py) ... done
    Building wheels for collected packages: python-whois, future
      Building wheel for python-whois (setup.py) ... done
      Created wheel for python-whois: filename=python_whois-0.8.0-py3-none-any.whl size=103247 sha256=d8f875938758411ec98a42a2569e8e7b05b0700e29c4b4804f0108520061fc70
      Stored in directory: c:\users\zhao song\appdata\local\pip\cache\wheels\e6\e9\d3\1e41a6c95b398de12c5a332ff28805aa44e68aa317ea60266d
      Building wheel for future (setup.py) ... done
      Created wheel for future: filename=future-0.18.2-py3-none-any.whl size=491058 sha256=4c7b6ea9290f5bec20b9feef8b23b2d9ff6ab343ae685a2a5698447a161cfe51
      Stored in directory: c:\users\zhao song\appdata\local\pip\cache\wheels\2f\a0\d3\4030d9f80e6b3be787f19fc911b8e7aa462986a40ab1e4bb94
    Successfully built python-whois future
    Installing collected packages: future, python-whois
      WARNING: The scripts futurize.exe and pasteurize.exe are installed in 'C:\Users\Zhao Song\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed future-0.18.2 python-whois-0.8.0
    
  2. Run the command pip show python-whois to confirm the installation is successful and get the installation directory.
    > pip show python-whois
    Name: python-whois
    Version: 0.8.0
    Summary: Whois querying and parsing of domain registration information.
    Home-page: https://github.com/richardpenman/whois
    Author: Richard Penman
    Author-email: richard.penman@gmail.com
    License: MIT
    Location: c:\users\zhao song\appdata\roaming\python\python39\site-packages
    Requires: future
    Required-by:

2. How To Use python-whois Module To Get Domain Whois Information.

  1. Now you can get a domain’s whois information in python using the python-whois module.
  2. Open a terminal and enter the command python to go to the python interpreter console.
  3. First, run the command import whois to import the python-whois module.
  4. Then you can invoke the python-whois module’s whois function to get a domain whois information like below.
    >>> import whois
    >>>
    >>> whois.whois('www.dev2qa.com')
    {'domain_name': 'DEV2QA.COM', 'registrar': 'FastDomain Inc.', 'whois_server': 'whois.bluehost.com', 'referral_url': None, 'updated_date': datetime.datetime(2022, 1, 26, 2, 38, 50), 'creation_date': datetime.datetime(2017, 2, 9, 10, 38, 18), 'expiration_date': datetime.datetime(2023, 2, 9, 10, 38, 18), 'name_servers': ['CRUZ.NS.CLOUDFLARE.COM', 'VIN.NS.CLOUDFLARE.COM'], 'status': 'clientTransferProhibited https://icann.org/epp#clientTransferProhibited', 'emails': ['tos@fastdomain.com', 'HAPPYZHAOSONG@GMAIL.COM', 'SUPPORT-DOMAIN@BLUEHOST.COM'], 'dnssec': 'unsigned', 'name': 'SONG ZHAO', 'org': None, 'address': 'CHANG PING', 'city': 'BEI JING', 'state': None, 'registrant_postal_code': '102208', 'country': 'CN'}
    >>>
  5. The whois function returns a dictionary object, you can get the dictionary object’s keys and values like below.
    # The whois.whois function reutrns a dictionary object.
    >>> whois_dict = whois.whois('www.dev2qa.com')
    >>>
    # Print out the dictionary object's keys and values.
    >>> print(whois_dict)
    {
      "domain_name": "DEV2QA.COM",
      "registrar": "FastDomain Inc.",
      "whois_server": "whois.bluehost.com",
      "referral_url": null,
      "updated_date": "2022-01-26 02:38:50",
      "creation_date": "2017-02-09 10:38:18",
      "expiration_date": "2023-02-09 10:38:18",
      "name_servers": [
        "CRUZ.NS.CLOUDFLARE.COM",
        "VIN.NS.CLOUDFLARE.COM"
      ],
      "status": "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
      "emails": [
        "tos@fastdomain.com",
        "HAPPYZHAOSONG@GMAIL.COM",
        "SUPPORT-DOMAIN@BLUEHOST.COM"
      ],
      "dnssec": "unsigned",
      "name": "SONG ZHAO",
      "org": null,
      "address": "CHANG PING",
      "city": "BEI JING",
      "state": null,
      "registrant_postal_code": "102208",
      "country": "CN"
    }
    >>>
    # Get the domain expiration date time through the whois object's expiration_date attribute.
    >>> whois_dict.expiration_date
    datetime.datetime(2023, 2, 9, 10, 38, 18)
    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.