Skip to content

Instantly share code, notes, and snippets.

@silvio2402
Last active August 12, 2022 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silvio2402/0e8b6de1494ce6cf0408a8ac337c14d6 to your computer and use it in GitHub Desktop.
Save silvio2402/0e8b6de1494ce6cf0408a8ac337c14d6 to your computer and use it in GitHub Desktop.
Query WHOIS information from IANA server
import socket
def whois(cmd: str, whois_server="whois.iana.org", port=43):
# Connect to the service host
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((whois_server, port))
# Send a single "command line", ending with <CRLF>.
sock.send(cmd.encode("utf-8"))
sock.send("\r\n".encode("utf-8"))
# Receive information in response to the command line.
msg = ""
data = sock.recv(1024)
while data:
msg += data.decode("utf-8")
data = sock.recv(1024)
groups = []
currGroup = {}
for line in msg.splitlines():
if line.startswith("%"): # Skip comments
continue
if line == "": # Blank line refers to start of new group
if currGroup != {}:
groups.append(currGroup)
currGroup = {}
continue
kv = line.split(":", 1) # Split line to key/value pair
key = kv[0].lstrip() # Remove leading spaces
value = kv[1].lstrip() # Remove leading spaces
if key in currGroup:
# If key already exists, join values by newline
currGroup[key] = "\n".join([currGroup[key], value])
else:
currGroup[key] = value
return groups
if __name__ == "__main__":
import json
s = whois("google.com")
print(json.dumps(s, indent=4))
[
{
"refer": "whois.verisign-grs.com"
},
{
"domain": "COM"
},
{
"organisation": "VeriSign Global Registry Services",
"address": "12061 Bluemont Way\nReston Virginia 20190\nUnited States"
},
{
"contact": "administrative",
"name": "Registry Customer Service",
"organisation": "VeriSign Global Registry Services",
"address": "12061 Bluemont Way\nReston Virginia 20190\nUnited States",
"phone": "+1 703 925-6999",
"fax-no": "+1 703 948 3978",
"e-mail": "info@verisign-grs.com"
},
{
"contact": "technical",
"name": "Registry Customer Service",
"organisation": "VeriSign Global Registry Services",
"address": "12061 Bluemont Way\nReston Virginia 20190\nUnited States",
"phone": "+1 703 925-6999",
"fax-no": "+1 703 948 3978",
"e-mail": "info@verisign-grs.com"
},
{
"nserver": "A.GTLD-SERVERS.NET 192.5.6.30 2001:503:a83e:0:0:0:2:30\nB.GTLD-SERVERS.NET 192.33.14.30 2001:503:231d:0:0:0:2:30\nC.GTLD-SERVERS.NET 192.26.92.30 2001:503:83eb:0:0:0:0:30\nD.GTLD-SERVERS.NET 192.31.80.30 2001:500:856e:0:0:0:0:30\nE.GTLD-SERVERS.NET 192.12.94.30 2001:502:1ca1:0:0:0:0:30\nF.GTLD-SERVERS.NET 192.35.51.30 2001:503:d414:0:0:0:0:30\nG.GTLD-SERVERS.NET 192.42.93.30 2001:503:eea3:0:0:0:0:30\nH.GTLD-SERVERS.NET 192.54.112.30 2001:502:8cc:0:0:0:0:30\nI.GTLD-SERVERS.NET 192.43.172.30 2001:503:39c1:0:0:0:0:30\nJ.GTLD-SERVERS.NET 192.48.79.30 2001:502:7094:0:0:0:0:30\nK.GTLD-SERVERS.NET 192.52.178.30 2001:503:d2d:0:0:0:0:30\nL.GTLD-SERVERS.NET 192.41.162.30 2001:500:d937:0:0:0:0:30\nM.GTLD-SERVERS.NET 192.55.83.30 2001:501:b1f9:0:0:0:0:30",
"ds-rdata": "30909 8 2 E2D3C916F6DEEAC73294E8268FB5885044A833FC5459588F4A9184CFC41A5766"
},
{
"whois": "whois.verisign-grs.com"
},
{
"status": "ACTIVE",
"remarks": "Registration information: http://www.verisigninc.com"
},
{
"created": "1985-01-01",
"changed": "2017-10-05",
"source": "IANA"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment