Last active
October 24, 2019 06:58
-
-
Save dpetzold/2d75ae62404a6a906ba581ea88c48123 to your computer and use it in GitHub Desktop.
Query whois.radb.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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