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