Skip to content

Instantly share code, notes, and snippets.

@dpetzold
Last active October 24, 2019 06:58
Show Gist options
  • Save dpetzold/2d75ae62404a6a906ba581ea88c48123 to your computer and use it in GitHub Desktop.
Save dpetzold/2d75ae62404a6a906ba581ea88c48123 to your computer and use it in GitHub Desktop.
Query whois.radb.net
import functools
import logging
import socket
import whois
logger = logging.getLogger(__name__)
@functools.lru_cache(maxsize=1)
def gethostbyname(hostname: str) -> str:
return socket.gethostbyname(hostname)
@functools.lru_cache(maxsize=16384)
def query_radb(ip_address: str) -> dict:
try:
query = whois.NICClient().whois(
ip_address,
gethostbyname('whois.radb.net'),
0,
)
except (
socket.gaierror,
socket.herror,
socket.timeout,
ConnectionResetError,
) as exc:
logger.error(f'{ip_address} lookup failed: {str(exc)}')
return {}
d = {}
for line in query.split('\n'):
if ':' not in line:
continue
s = line.split(':', 1)
d[s[0].strip()] = s[1].strip()
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment