Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 6 min read

4 Ways to Do a WHOIS Lookup From Code (Tested on 5 Real Domains)

Getting WHOIS data into a script sounds like a one-liner until you hit a TLD that doesn't behave. I tried the four usual routes against the same list of domains and saved what each one printed. The list is small but mea

4 Ways to Do a WHOIS Lookup From Code (Tested on 5 Real Domains)

Getting WHOIS data into a script sounds like a one-liner until you hit a TLD that doesn't behave. I tried the four usual routes against the same list of domains and saved what each one printed.

The list is small but mean:

github.com    lemonde.fr    socket.io    spiegel.de    bbc.co.uk

One boring .com and four country codes from different registries. Everything below ran on 4 December 2026 on Ubuntu, except the API call at the end.

1. Shelling out to whois

Preinstalled on macOS, one apt install whois away on Debian and Ubuntu. It speaks the old port-43 protocol and hands you plain text.

for d in github.com lemonde.fr socket.io spiegel.de bbc.co.uk; do
  echo "== $d"
  whois "$d" | grep -iE 'registrar:|creation date|created:|registered on|expir' | head -4
done

The whois loop and its output for the five domains, with four problems flagged

The same output as text:

== github.com
   Creation Date: 2007-10-09T18:20:50Z
   Registry Expiry Date: 2028-10-09T18:20:50Z
   Registrar: MarkMonitor Inc.
NOTICE: The expiration date displayed in this record is the date the
== lemonde.fr
registrar:                     NAMESHIELD
Expiry Date:                   2027-06-09T22:08:09Z
created:                       2005-08-02T14:16:36Z
registrar:                     NAMESHIELD
== socket.io
Creation Date: 2010-04-18T22:40:06Z
Registry Expiry Date: 2027-04-18T22:40:06Z
Registrar: MarkMonitor Inc.
== spiegel.de
== bbc.co.uk
    Registrar:
        Registered on: before Aug-1996
        Expiry date:  13-Dec-2034

My grep is already sloppy after five domains. It grabbed a line of legal boilerplate for github.com and the registrar twice for lemonde.fr, because .fr repeats it in every contact block. spiegel.de matched nothing: Germany's registry only sends nameservers, a status and a changed date over port 43. And bbc.co.uk has no real date, just before Aug-1996.

Nominet also adds a banner to every .uk reply: WHOIS for .uk shuts down on 9 February 2027. So don't build anything long-lived on scraping it.

Good for: a quick look in a terminal. Painful for: anything that has to parse more than one registry.

2. RDAP with curl and jq

RDAP is WHOIS over HTTPS with JSON responses. For gTLDs it has been the official source since January 2025. You don't need to know which server to ask, because rdap.org redirects you.

for d in github.com lemonde.fr socket.io spiegel.de bbc.co.uk; do
  code=$(curl -sL -o "rdap-$d.json" -w '%{http_code}' "https://rdap.org/domain/$d")
  echo "== $d HTTP $code"
  [ "$code" = 200 ] && jq -c '{
    registrar: ([.entities[]? | select(.roles | index("registrar"))
                 | .vcardArray[1][]? | select(.[0] == "fn") | .[3]] | first),
    events: ([.events[]? | {(.eventAction): .eventDate}] | add)
  }' "rdap-$d.json"
done
== github.com HTTP 200
{"registrar":"MarkMonitor Inc.","events":{"registration":"2007-10-09T18:20:50Z","expiration":"2028-10-09T18:20:50Z","last changed":"2026-09-07T09:22:52Z","last update of RDAP database":"2026-12-04T04:49:43Z"}}
== lemonde.fr HTTP 200
{"registrar":"NAMESHIELD","events":{"registration":"2005-08-02T14:16:36Z","expiration":"2027-06-09T22:08:09Z","last changed":"2026-06-12T22:48:47.579297Z","transfer":"2022-06-09T22:08:09Z","last update of RDAP database":"2026-12-04T04:49:51.222453284Z"}}
== socket.io HTTP 404
== spiegel.de HTTP 404
== bbc.co.uk HTTP 200
{"registrar":"British Broadcasting Corporation","events":{"registration":"1994-12-13T03:49:48Z","last changed":"2025-10-29T03:51:11.009091Z","expiration":"2034-12-13T03:49:48Z","last update of RDAP database":"2026-12-04T04:49:53.471Z"}}

RDAP events for bbc.co.uk and 404 responses for socket.io and spiegel.de

One jq filter works for three different registries, which felt great after method 1. bbc.co.uk even gets its real registration date, 1994-12-13, instead of "before Aug-1996".

Then the two 404s. rdap.org had nothing for .io or .de, so 2 of my 5 domains just don't exist as far as this method is concerned.

Good for: clean, official data on gTLDs and the ccTLDs that support it. You'll still want a fallback.

3. The python-whois package

If you live in Python, python-whois wraps the WHOIS query and turns the text into attributes.

python3 -m venv .venv && . .venv/bin/activate
pip install python-whois
import whois

DOMAINS = ["github.com", "lemonde.fr", "socket.io", "spiegel.de", "bbc.co.uk"]

def first(value):
    # Some registries return lists for dates; take the first value.
    return value[0] if isinstance(value, list) else value

for domain in DOMAINS:
    try:
        w = whois.whois(domain)
        print(f"{domain:12} registrar={w.registrar!s:20} "
              f"created={first(w.creation_date)!s:26} expires={first(w.expiration_date)}")
    except Exception as exc:
        print(f"{domain:12} error: {type(exc).__name__}: {exc}")

With python-whois 0.9.6:

github.com   registrar=MarkMonitor Inc.     created=2007-10-09 18:20:50+00:00  expires=2028-10-09 18:20:50+00:00
lemonde.fr   registrar=NAMESHIELD           created=2005-08-02 14:16:36+00:00  expires=2027-06-09 22:08:09+00:00
socket.io    registrar=MarkMonitor Inc.     created=2010-04-18 22:40:06+00:00  expires=2027-04-18 22:40:06+00:00
spiegel.de   registrar=None                 created=None                       expires=None
bbc.co.uk    error: WhoisError: Whois command returned no output

Real datetime objects for three domains, which is nice. spiegel.de gives you a row of Nones, since there was nothing to parse. bbc.co.uk threw WhoisError on all three of my runs, even though the plain whois command answered fine. Wrap it in try or one odd TLD will kill your batch job.

Good for: small Python scripts where None is an acceptable answer.

4. A WHOIS API

The last option hands the registry mess to someone else. You send a domain, you get back the same JSON shape whatever the TLD. Full disclosure, I work at WhoisFreaks, so that's the API I used. Other WHOIS APIs follow a similar request pattern. A free key comes with 500 credits and no card, and this walkthrough of the WhoisFreaks API shows where to get one.

Put the key in an environment variable, not in the file:

export WHOISFREAKS_API_KEY="your-key-here"
pip install requests
import os
import time

import requests

API_KEY = os.environ["WHOISFREAKS_API_KEY"]
DOMAINS = ["github.com", "lemonde.fr", "socket.io", "spiegel.de", "bbc.co.uk"]

for domain in DOMAINS:
    r = requests.get(
        "https://api.whoisfreaks.com/v1.0/whois",
        params={"apiKey": API_KEY, "whois": "live", "domainName": domain},
        timeout=30,
    )
    if r.ok:
        data = r.json()
        registrar = (data.get("domain_registrar") or {}).get("registrar_name")
        print(f"{domain:12} registrar={registrar!s:30} "
              f"created={data.get('create_date')}  expires={data.get('expiry_date')}")
    else:
        print(f"{domain:12} HTTP {r.status_code}")
    time.sleep(6)  # the free tier allows 10 live lookups a minute
github.com   registrar=Markmonitor Inc                created=2007-10-09  expires=2028-10-09
lemonde.fr   registrar=NAMESHIELD                     created=2005-08-02  expires=2027-06-09
socket.io    registrar=MarkMonitor Inc                created=2010-04-18  expires=2027-04-18
spiegel.de   registrar=Key-Systems GmbH               created=1994-10-26  expires=None
bbc.co.uk    registrar=British Broadcasting Corporation created=1994-12-13  expires=2034-12-13

When I pushed the same five domains through this API with a free key on 26 November, every one came back with a registrar and dates. That includes spiegel.de: Key-Systems GmbH, created 1994-10-26, which methods 1 to 3 couldn't get. It had no expiry date, which is right, because DENIC doesn't publish one.

It isn't perfect. You spend credits once the free ones run out, the free tier is rate limited, and in our own testing a couple of domains (producthunt.com, rakuten.co.jp) came back without a registrar. We've logged those.

Good for: batch jobs across many TLDs, or when you need the fields the registries don't publish.

Scoreboard

Scoreboard table with each domain's result per method: whois CLI 4 of 5, RDAP 3 of 5, python-whois 3 of 5, WHOIS API 5 of 5

whois RDAP + curl python-whois API
Cost Free Free Free Free tier, then paid
Output Text JSON Python objects JSON
One format for every TLD No Where RDAP exists Mostly Yes
Domains with registrar or dates 4/5 3/5 3/5 5/5
Exact bbc.co.uk created date No Yes No (error) Check your run

If I were wiring this up from scratch: RDAP first, since it's free and official, whois as the fallback for ccTLDs RDAP can't see, and an API for batch jobs where a missing registrar is a real problem. For a single domain with no code at all, the WhoisFreaks lookup page does the same query in a browser.

Got a TLD that breaks your WHOIS code? Drop it in the comments and I'll add it to the list.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.