| author | Richard Penman |
| Fri, 20 Jan 2017 15:33:38 +0800 | |
| changeset 122 | 95feee1af1da |
| parent 98 | 3202436d89d0 |
| child 123 | 03c72d0d1182 |
| permissions | -rw-r--r-- |
| 70 | 1 |
from __future__ import print_function |
2 |
from __future__ import absolute_import |
|
3 |
from __future__ import unicode_literals |
|
4 |
from __future__ import division |
|
5 |
from future import standard_library |
|
6 |
standard_library.install_aliases() |
|
7 |
from builtins import * |
|
| 0 | 8 |
import re |
9 |
import sys |
|
|
29
1ebe960587b1
Read in all TLDs from a file
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
25
diff
changeset
|
10 |
import os |
| 0 | 11 |
import subprocess |
| 8 | 12 |
import socket |
| 70 | 13 |
from .parser import WhoisEntry |
14 |
from .whois import NICClient |
|
| 0 | 15 |
|
16 |
||
| 122 | 17 |
|
| 60 | 18 |
def whois(url, command=False): |
| 0 | 19 |
# clean domain to expose netloc |
| 25 | 20 |
ip_match = re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", url)
|
21 |
if ip_match: |
|
22 |
domain = url |
|
|
86
d6fcfa5acc7b
added support for resolving IP addresses to domain when has a PTR record
Richard Penman
parents:
73
diff
changeset
|
23 |
try: |
|
d6fcfa5acc7b
added support for resolving IP addresses to domain when has a PTR record
Richard Penman
parents:
73
diff
changeset
|
24 |
result = socket.gethostbyaddr(url) |
|
d6fcfa5acc7b
added support for resolving IP addresses to domain when has a PTR record
Richard Penman
parents:
73
diff
changeset
|
25 |
except socket.herror as e: |
|
d6fcfa5acc7b
added support for resolving IP addresses to domain when has a PTR record
Richard Penman
parents:
73
diff
changeset
|
26 |
pass |
|
d6fcfa5acc7b
added support for resolving IP addresses to domain when has a PTR record
Richard Penman
parents:
73
diff
changeset
|
27 |
else: |
|
d6fcfa5acc7b
added support for resolving IP addresses to domain when has a PTR record
Richard Penman
parents:
73
diff
changeset
|
28 |
domain = result[0] |
| 25 | 29 |
else: |
30 |
domain = extract_domain(url) |
|
| 60 | 31 |
if command: |
32 |
# try native whois command |
|
33 |
r = subprocess.Popen(['whois', domain], stdout=subprocess.PIPE) |
|
34 |
text = r.stdout.read() |
|
|
33
8c4c05eb65f4
Allow explicit usage of NICClient even if whois binary is available
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
31
diff
changeset
|
35 |
else: |
| 60 | 36 |
# try builtin client |
| 0 | 37 |
nic_client = NICClient() |
| 122 | 38 |
text = nic_client.whois_lookup(None, domain.encode('idna'), 0)
|
| 0 | 39 |
return WhoisEntry.load(domain, text) |
40 |
||
| 5 | 41 |
|
| 0 | 42 |
def extract_domain(url): |
43 |
"""Extract the domain from the given URL |
|
44 |
||
45 |
>>> extract_domain('http://www.google.com.au/tos.html')
|
|
46 |
'google.com.au' |
|
| 60 | 47 |
>>> extract_domain('www.webscraping.com')
|
| 11 | 48 |
'webscraping.com' |
| 22 | 49 |
>>> extract_domain('198.252.206.140')
|
| 8 | 50 |
'stackoverflow.com' |
| 22 | 51 |
>>> extract_domain('102.112.2O7.net')
|
52 |
'2o7.net' |
|
53 |
>>> extract_domain('1-0-1-1-1-0-1-1-1-1-1-1-1-.0-0-0-0-0-0-0-0-0-0-0-0-0-10-0-0-0-0-0-0-0-0-0-0-0-0-0.info')
|
|
54 |
'0-0-0-0-0-0-0-0-0-0-0-0-0-10-0-0-0-0-0-0-0-0-0-0-0-0-0.info' |
|
| 0 | 55 |
""" |
| 22 | 56 |
if re.match(r'\d+\.\d+\.\d+\.\d+', url): |
| 8 | 57 |
# this is an IP address |
58 |
return socket.gethostbyaddr(url)[0] |
|
| 11 | 59 |
|
|
31
92176112c2d6
Move tlds.txt to a data/ folder and add it to the package
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
29
diff
changeset
|
60 |
tlds_path = os.path.join(os.getcwd(), os.path.dirname(__file__), 'data', 'tlds.txt') |
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
61 |
with open(tlds_path) as tlds_fil: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
62 |
suffixes = [line.lower().encode('utf-8')
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
63 |
for line in (x.strip() for x in tlds_fil) |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
64 |
if not line.startswith('#')]
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
65 |
suff = 'xn--p1ai' |
|
29
1ebe960587b1
Read in all TLDs from a file
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
25
diff
changeset
|
66 |
|
| 71 | 67 |
if not isinstance(url, str): |
|
38
da8f2956db7e
Ensure lookups work with both unicode and bytes objects
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
35
diff
changeset
|
68 |
url = url.decode('utf-8')
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
69 |
url = re.sub('^.*://', '', url)
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
70 |
url = url.split('/')[0].lower().encode('idna')
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
73
diff
changeset
|
71 |
|
| 0 | 72 |
domain = [] |
| 71 | 73 |
for section in url.split(b'.'): |
| 0 | 74 |
if section in suffixes: |
75 |
domain.append(section) |
|
76 |
else: |
|
77 |
domain = [section] |
|
| 71 | 78 |
return b'.'.join(domain).decode('idna')
|
| 0 | 79 |
|
80 |
||
81 |
if __name__ == '__main__': |
|
82 |
try: |
|
83 |
url = sys.argv[1] |
|
84 |
except IndexError: |
|
| 70 | 85 |
print('Usage: %s url' % sys.argv[0])
|
| 0 | 86 |
else: |
| 70 | 87 |
print(whois(url)) |