forked from lucasayres/python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhois.py
More file actions
35 lines (29 loc) · 774 Bytes
/
whois.py
File metadata and controls
35 lines (29 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
import ipaddress
from warnings import filterwarnings
from ipwhois import IPWhois
from urllib import parse
from dns import resolver
filterwarnings(action='ignore')
def whois(url):
"""Get WHOIS from IP address or hostname.
Args:
url (str): IP address or hostname.
Returns:
dict: Return a JSON object with the WHOIS.
"""
url = url.strip()
if not parse.urlparse(url).scheme:
url = 'http://' + url
host = parse.urlparse(url).netloc
try:
ipaddress.ip_address(host)
ip = host
except Exception:
try:
ips = resolver.query(host, 'A')
ip = ips[0]
except Exception:
return {}
obj = IPWhois(ip)
return obj.lookup_whois()