| author | richardpenman |
| Mon, 05 Feb 2018 14:43:20 -0500 | |
| changeset 160 | c385321ec3e4 |
| parent 136 | 30259bf0523f |
| child 163 | faaa3ce1af3e |
| permissions | -rw-r--r-- |
| 123 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
| 0 | 3 |
""" |
4 |
Whois client for python |
|
5 |
||
6 |
transliteration of: |
|
7 |
http://www.opensource.apple.com/source/adv_cmds/adv_cmds-138.1/whois/whois.c |
|
8 |
||
9 |
Copyright (c) 2010 Chris Wolf |
|
10 |
||
11 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
12 |
of this software and associated documentation files (the "Software"), to deal |
|
13 |
in the Software without restriction, including without limitation the rights |
|
14 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
15 |
copies of the Software, and to permit persons to whom the Software is |
|
16 |
furnished to do so, subject to the following conditions: |
|
17 |
||
18 |
The above copyright notice and this permission notice shall be included in |
|
19 |
all copies or substantial portions of the Software. |
|
20 |
||
21 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
22 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
23 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
24 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
25 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
26 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
27 |
THE SOFTWARE. |
|
28 |
""" |
|
| 70 | 29 |
from __future__ import print_function |
30 |
from __future__ import unicode_literals |
|
31 |
from __future__ import division |
|
32 |
from __future__ import absolute_import |
|
33 |
from future import standard_library |
|
34 |
standard_library.install_aliases() |
|
35 |
from builtins import * |
|
36 |
from builtins import object |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
37 |
import re |
| 0 | 38 |
import sys |
39 |
import socket |
|
40 |
import optparse |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
41 |
|
| 0 | 42 |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
43 |
class NICClient(object): |
| 0 | 44 |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
45 |
ABUSEHOST = "whois.abuse.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
46 |
NICHOST = "whois.crsnic.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
47 |
INICHOST = "whois.networksolutions.com" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
48 |
DNICHOST = "whois.nic.mil" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
49 |
GNICHOST = "whois.nic.gov" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
50 |
ANICHOST = "whois.arin.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
51 |
LNICHOST = "whois.lacnic.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
52 |
RNICHOST = "whois.ripe.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
53 |
PNICHOST = "whois.apnic.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
54 |
MNICHOST = "whois.ra.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
55 |
QNICHOST_TAIL = ".whois-servers.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
56 |
SNICHOST = "whois.6bone.net" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
57 |
BNICHOST = "whois.registro.br" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
58 |
NORIDHOST = "whois.norid.no" |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
59 |
IANAHOST = "whois.iana.org" |
|
112
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
60 |
PANDIHOST = "whois.pandi.or.id" |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
61 |
DENICHOST = "de.whois-servers.net" |
|
133
29e9190319e5
Added support for .ai domains
soulmachine <soulmachine@gmail.com>
parents:
124
diff
changeset
|
62 |
AI_HOST = "whois.ai" |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
63 |
DEFAULT_PORT = "nicname" |
| 0 | 64 |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
65 |
WHOIS_RECURSE = 0x01 |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
66 |
WHOIS_QUICK = 0x02 |
| 0 | 67 |
|
|
112
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
68 |
ip_whois = [LNICHOST, RNICHOST, PNICHOST, BNICHOST,PANDIHOST] |
| 0 | 69 |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
70 |
def __init__(self): |
| 0 | 71 |
self.use_qnichost = False |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
72 |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
73 |
def findwhois_server(self, buf, hostname, query): |
| 0 | 74 |
"""Search the initial TLD lookup results for the regional-specifc |
75 |
whois server for getting contact details. |
|
76 |
""" |
|
77 |
nhost = None |
|
| 160 | 78 |
match = re.compile('Domain Name: {}\s*.*?Whois Server: (.*?)\s'.format(query), flags=re.IGNORECASE|re.DOTALL).search(buf)
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
79 |
if match: |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
80 |
nhost = match.groups()[0] |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
81 |
# if the whois address is domain.tld/something then |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
82 |
# s.connect((hostname, 43)) does not work |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
83 |
if nhost.count('/') > 0:
|
|
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
84 |
nhost = None |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
85 |
elif hostname == NICClient.ANICHOST: |
| 0 | 86 |
for nichost in NICClient.ip_whois: |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
87 |
if buf.find(nichost) != -1: |
| 0 | 88 |
nhost = nichost |
89 |
break |
|
90 |
return nhost |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
91 |
|
| 122 | 92 |
|
|
64
2ed54e885571
only focus whois search after find many results, to avoid search failure
Richard Penman
parents:
62
diff
changeset
|
93 |
def whois(self, query, hostname, flags, many_results=False): |
| 0 | 94 |
"""Perform initial lookup with TLD whois server |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
95 |
then, if the quick flag is false, search that result |
| 0 | 96 |
for the region-specifc whois server and do a lookup |
97 |
there for contact details |
|
98 |
""" |
|
|
105
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
99 |
response = b'' |
|
52
1f80f6dec7ac
add socket timeout exception
Jasey Wang <jaseywang@gmail.com>
parents:
45
diff
changeset
|
100 |
try: |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
101 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
102 |
s.settimeout(10) |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
103 |
s.connect((hostname, 43)) |
|
105
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
104 |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
105 |
try: |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
106 |
query = query.decode('utf-8')
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
107 |
except UnicodeEncodeError: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
108 |
pass # Already Unicode (python2's error) |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
109 |
except AttributeError: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
110 |
pass # Already Unicode (python3's error) |
|
34
f9da616f15cf
Pass all domains through encode('idna') in NICClient
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
32
diff
changeset
|
111 |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
112 |
if hostname == NICClient.DENICHOST: |
|
105
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
113 |
query_bytes = "-T dn,ace -C UTF-8 " + query |
|
64
2ed54e885571
only focus whois search after find many results, to avoid search failure
Richard Penman
parents:
62
diff
changeset
|
114 |
elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results: |
| 124 | 115 |
query_bytes = '=' + query |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
116 |
else: |
|
105
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
117 |
query_bytes = query |
| 136 | 118 |
s.send(bytes(query_bytes,'utf-8') + b"\r\n") |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
119 |
# recv returns bytes |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
120 |
while True: |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
121 |
d = s.recv(4096) |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
122 |
response += d |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
123 |
if not d: |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
124 |
break |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
125 |
s.close() |
|
106
40e0392ef844
whois.py edited online with Bitbucket
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
105
diff
changeset
|
126 |
except socket.error as socketerror: |
| 70 | 127 |
print('Socket Error:', socketerror)
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
128 |
|
|
105
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
129 |
nhost = None |
|
132
7666f0765fe4
add .br expire date, fix decode() takes no keyword arguments
Jonatan Vianna <jonatan.vianna@gmail.com>
parents:
124
diff
changeset
|
130 |
response = response.decode('utf-8', 'replace')
|
|
105
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
131 |
if 'with "=xxx"' in response: |
|
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
132 |
return self.whois(query, hostname, flags, True) |
|
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
133 |
if flags & NICClient.WHOIS_RECURSE and nhost is None: |
|
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
134 |
nhost = self.findwhois_server(response, hostname, query) |
|
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
135 |
if nhost is not None: |
|
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
136 |
response += self.whois(query, nhost, 0) |
|
2228d503a1f0
socket error on the second chunk of data truncates all received data
Andrey Skabelin <andrey.skabelin@gmail.com>
parents:
97
diff
changeset
|
137 |
return response |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
138 |
|
| 0 | 139 |
def choose_server(self, domain): |
140 |
"""Choose initial lookup NIC host""" |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
141 |
try: |
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
142 |
domain = domain.encode('idna').decode('utf-8')
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
143 |
except TypeError: |
| 70 | 144 |
domain = domain.decode('utf-8').encode('idna').decode('utf-8')
|
| 136 | 145 |
except AttributeError: |
146 |
domain = domain.decode('utf-8').encode('idna').decode('utf-8')
|
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
147 |
if domain.endswith("-NORID"):
|
| 0 | 148 |
return NICClient.NORIDHOST |
|
112
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
149 |
if domain.endswith("id"):
|
|
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
150 |
return NICClient.PANDIHOST |
|
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
151 |
|
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
152 |
domain = domain.split('.')
|
|
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
153 |
if len(domain) < 2: |
| 0 | 154 |
return None |
|
97
44522cd37b07
Fixed tests. Also some UTF bugs (python2/3 hell)
Mario D. Santana <mario@elorangutan.com>
parents:
90
diff
changeset
|
155 |
tld = domain[-1] |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
156 |
if tld[0].isdigit(): |
| 0 | 157 |
return NICClient.ANICHOST |
|
133
29e9190319e5
Added support for .ai domains
soulmachine <soulmachine@gmail.com>
parents:
124
diff
changeset
|
158 |
elif tld == 'ai': |
|
29e9190319e5
Added support for .ai domains
soulmachine <soulmachine@gmail.com>
parents:
124
diff
changeset
|
159 |
return NICClient.AI_HOST |
|
29e9190319e5
Added support for .ai domains
soulmachine <soulmachine@gmail.com>
parents:
124
diff
changeset
|
160 |
else: |
|
29e9190319e5
Added support for .ai domains
soulmachine <soulmachine@gmail.com>
parents:
124
diff
changeset
|
161 |
return tld + NICClient.QNICHOST_TAIL |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
162 |
|
| 0 | 163 |
def whois_lookup(self, options, query_arg, flags): |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
164 |
"""Main entry point: Perform initial lookup on TLD whois server, |
|
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
165 |
or other server to get region-specific whois server, then if quick |
|
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
166 |
flag is false, perform a second lookup on the region-specific |
| 0 | 167 |
server for contact records""" |
168 |
nichost = None |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
169 |
# whoud happen when this function is called by other than main |
| 60 | 170 |
if options is None: |
| 0 | 171 |
options = {}
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
172 |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
173 |
if ('whoishost' not in options or options['whoishost'] is None) \
|
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
174 |
and ('country' not in options or options['country'] is None):
|
| 0 | 175 |
self.use_qnichost = True |
176 |
options['whoishost'] = NICClient.NICHOST |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
177 |
if not (flags & NICClient.WHOIS_QUICK): |
| 0 | 178 |
flags |= NICClient.WHOIS_RECURSE |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
179 |
|
| 60 | 180 |
if 'country' in options and options['country'] is not None: |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
181 |
result = self.whois( |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
182 |
query_arg, |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
183 |
options['country'] + NICClient.QNICHOST_TAIL, |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
184 |
flags |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
185 |
) |
| 60 | 186 |
elif self.use_qnichost: |
| 0 | 187 |
nichost = self.choose_server(query_arg) |
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
188 |
if nichost is not None: |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
189 |
result = self.whois(query_arg, nichost, flags) |
|
45
52ce01013731
Handle experimental lookup method for bad domains
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
36
diff
changeset
|
190 |
else: |
|
52ce01013731
Handle experimental lookup method for bad domains
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
36
diff
changeset
|
191 |
result = '' |
| 0 | 192 |
else: |
193 |
result = self.whois(query_arg, options['whoishost'], flags) |
|
194 |
return result |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
195 |
|
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
196 |
|
| 0 | 197 |
def parse_command_line(argv): |
198 |
"""Options handling mostly follows the UNIX whois(1) man page, except |
|
199 |
long-form options can also be used. |
|
200 |
""" |
|
201 |
flags = 0 |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
202 |
|
| 0 | 203 |
usage = "usage: %prog [options] name" |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
204 |
|
| 0 | 205 |
parser = optparse.OptionParser(add_help_option=False, usage=usage) |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
206 |
parser.add_option("-a", "--arin", action="store_const",
|
| 0 | 207 |
const=NICClient.ANICHOST, dest="whoishost", |
208 |
help="Lookup using host " + NICClient.ANICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
209 |
parser.add_option("-A", "--apnic", action="store_const",
|
| 0 | 210 |
const=NICClient.PNICHOST, dest="whoishost", |
211 |
help="Lookup using host " + NICClient.PNICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
212 |
parser.add_option("-b", "--abuse", action="store_const",
|
| 0 | 213 |
const=NICClient.ABUSEHOST, dest="whoishost", |
214 |
help="Lookup using host " + NICClient.ABUSEHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
215 |
parser.add_option("-c", "--country", action="store",
|
| 0 | 216 |
type="string", dest="country", |
217 |
help="Lookup using country-specific NIC") |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
218 |
parser.add_option("-d", "--mil", action="store_const",
|
| 0 | 219 |
const=NICClient.DNICHOST, dest="whoishost", |
220 |
help="Lookup using host " + NICClient.DNICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
221 |
parser.add_option("-g", "--gov", action="store_const",
|
| 0 | 222 |
const=NICClient.GNICHOST, dest="whoishost", |
223 |
help="Lookup using host " + NICClient.GNICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
224 |
parser.add_option("-h", "--host", action="store",
|
| 0 | 225 |
type="string", dest="whoishost", |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
226 |
help="Lookup using specified whois host") |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
227 |
parser.add_option("-i", "--nws", action="store_const",
|
| 0 | 228 |
const=NICClient.INICHOST, dest="whoishost", |
229 |
help="Lookup using host " + NICClient.INICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
230 |
parser.add_option("-I", "--iana", action="store_const",
|
| 0 | 231 |
const=NICClient.IANAHOST, dest="whoishost", |
232 |
help="Lookup using host " + NICClient.IANAHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
233 |
parser.add_option("-l", "--lcanic", action="store_const",
|
| 0 | 234 |
const=NICClient.LNICHOST, dest="whoishost", |
235 |
help="Lookup using host " + NICClient.LNICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
236 |
parser.add_option("-m", "--ra", action="store_const",
|
| 0 | 237 |
const=NICClient.MNICHOST, dest="whoishost", |
238 |
help="Lookup using host " + NICClient.MNICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
239 |
parser.add_option("-p", "--port", action="store",
|
| 0 | 240 |
type="int", dest="port", |
241 |
help="Lookup using specified tcp port") |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
242 |
parser.add_option("-Q", "--quick", action="store_true",
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
243 |
dest="b_quicklookup", |
|
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
244 |
help="Perform quick lookup") |
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
245 |
parser.add_option("-r", "--ripe", action="store_const",
|
| 0 | 246 |
const=NICClient.RNICHOST, dest="whoishost", |
247 |
help="Lookup using host " + NICClient.RNICHOST) |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
248 |
parser.add_option("-R", "--ru", action="store_const",
|
| 0 | 249 |
const="ru", dest="country", |
250 |
help="Lookup Russian NIC") |
|
|
6
7dee244ba3ef
fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents:
0
diff
changeset
|
251 |
parser.add_option("-6", "--6bone", action="store_const",
|
| 0 | 252 |
const=NICClient.SNICHOST, dest="whoishost", |
253 |
help="Lookup using host " + NICClient.SNICHOST) |
|
|
112
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
254 |
parser.add_option("-n", "--ina", action="store_const",
|
|
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
255 |
const=NICClient.PANDIHOST, dest="whoishost", |
|
8acab8765146
Add Indonesian TLD(*.id) domain support whois server
Aan <cacaddv@gmail.com>
parents:
108
diff
changeset
|
256 |
help="Lookup using host " + NICClient.PANDIHOST) |
| 0 | 257 |
parser.add_option("-?", "--help", action="help")
|
258 |
||
259 |
return parser.parse_args(argv) |
|
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
260 |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
261 |
|
| 0 | 262 |
if __name__ == "__main__": |
263 |
flags = 0 |
|
264 |
nic_client = NICClient() |
|
|
62
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
265 |
options, args = parse_command_line(sys.argv) |
|
fc06c601b875
whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents:
60
diff
changeset
|
266 |
if options.b_quicklookup: |
|
32
5f851e9c196a
Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents:
12
diff
changeset
|
267 |
flags = flags | NICClient.WHOIS_QUICK |
| 70 | 268 |
print(nic_client.whois_lookup(options.__dict__, args[1], flags)) |