|
0
|
1 |
# parser.py - Module for parsing whois response data
|
|
|
2 |
# Copyright (c) 2008 Andrey Petrov
|
|
|
3 |
#
|
|
|
4 |
# This module is part of pywhois and is released under
|
|
|
5 |
# the MIT license: http://www.opensource.org/licenses/mit-license.php
|
|
|
6 |
|
|
|
7 |
import re
|
|
2
|
8 |
from datetime import datetime
|
|
0
|
9 |
|
|
|
10 |
|
|
|
11 |
class PywhoisError(Exception):
|
|
|
12 |
pass
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def cast_date(date_str):
|
|
|
16 |
"""Convert any date string found in WHOIS to a time object.
|
|
|
17 |
"""
|
|
|
18 |
known_formats = [
|
|
|
19 |
'%d-%b-%Y', # 02-jan-2000
|
|
|
20 |
'%Y-%m-%d', # 2000-01-02
|
|
2
|
21 |
'%d.%m.%Y', # 2000-01-02
|
|
0
|
22 |
'%d-%b-%Y %H:%M:%S %Z', # 24-Jul-2009 13:20:03 UTC
|
|
|
23 |
'%a %b %d %H:%M:%S %Z %Y', # Tue Jun 21 23:59:59 GMT 2011
|
|
|
24 |
'%Y-%m-%dT%H:%M:%SZ', # 2007-01-26T19:10:31Z
|
|
|
25 |
]
|
|
|
26 |
|
|
|
27 |
for format in known_formats:
|
|
|
28 |
try:
|
|
2
|
29 |
return datetime.strptime(date_str.strip(), format)
|
|
0
|
30 |
except ValueError, e:
|
|
|
31 |
pass # Wrong format, keep trying
|
|
|
32 |
return None
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
class WhoisEntry(object):
|
|
|
36 |
"""Base class for parsing a Whois entries.
|
|
|
37 |
"""
|
|
|
38 |
# regular expressions to extract domain data from whois profile
|
|
|
39 |
# child classes will override this
|
|
|
40 |
_regex = {
|
|
|
41 |
'domain_name': 'Domain Name:\s?(.+)',
|
|
|
42 |
'registrar': 'Registrar:\s?(.+)',
|
|
|
43 |
'whois_server': 'Whois Server:\s?(.+)',
|
|
|
44 |
'referral_url': 'Referral URL:\s?(.+)', # http url of whois_server
|
|
|
45 |
'updated_date': 'Updated Date:\s?(.+)',
|
|
|
46 |
'creation_date': 'Creation Date:\s?(.+)',
|
|
|
47 |
'expiration_date': 'Expiration Date:\s?(.+)',
|
|
|
48 |
'name_servers': 'Name Server:\s?(.+)', # list of name servers
|
|
|
49 |
'status': 'Status:\s?(.+)', # list of statuses
|
|
|
50 |
'emails': '[\w.-]+@[\w.-]+\.[\w]{2,4}', # list of email addresses
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
def __init__(self, domain, text, regex=None):
|
|
|
54 |
self.domain = domain
|
|
|
55 |
self.text = text
|
|
|
56 |
if regex is not None:
|
|
|
57 |
self._regex = regex
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
def __getattr__(self, attr):
|
|
|
61 |
"""The first time an attribute is called it will be calculated here.
|
|
|
62 |
The attribute is then set to be accessed directly by subsequent calls.
|
|
|
63 |
"""
|
|
|
64 |
whois_regex = self._regex.get(attr)
|
|
|
65 |
if whois_regex:
|
|
|
66 |
setattr(self, attr, re.findall(whois_regex, self.text))
|
|
|
67 |
return getattr(self, attr)
|
|
|
68 |
else:
|
|
|
69 |
raise KeyError('Unknown attribute: %s' % attr)
|
|
|
70 |
|
|
|
71 |
def __str__(self):
|
|
|
72 |
"""Print all whois properties of domain
|
|
|
73 |
"""
|
|
|
74 |
return '\n'.join('%s: %s' % (attr, str(getattr(self, attr))) for attr in self.attrs())
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
def attrs(self):
|
|
|
78 |
"""Return list of attributes that can be extracted for this domain
|
|
|
79 |
"""
|
|
|
80 |
return sorted(self._regex.keys())
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
@staticmethod
|
|
|
84 |
def load(domain, text):
|
|
|
85 |
"""Given whois output in ``text``, return an instance of ``WhoisEntry`` that represents its parsed contents.
|
|
|
86 |
"""
|
|
|
87 |
if text.strip() == 'No whois server is known for this kind of object.':
|
|
|
88 |
raise PywhoisError(text)
|
|
|
89 |
|
|
|
90 |
if '.com' in domain:
|
|
|
91 |
return WhoisCom(domain, text)
|
|
|
92 |
elif '.net' in domain:
|
|
|
93 |
return WhoisNet(domain, text)
|
|
|
94 |
elif '.org' in domain:
|
|
|
95 |
return WhoisOrg(domain, text)
|
|
|
96 |
elif '.ru' in domain:
|
|
|
97 |
return WhoisRu(domain, text)
|
|
|
98 |
elif '.name' in domain:
|
|
|
99 |
return WhoisName(domain, text)
|
|
|
100 |
elif '.us' in domain:
|
|
|
101 |
return WhoisUs(domain, text)
|
|
|
102 |
elif '.me' in domain:
|
|
|
103 |
return WhoisMe(domain, text)
|
|
|
104 |
elif '.uk' in domain:
|
|
|
105 |
return WhoisUk(domain, text)
|
|
2
|
106 |
elif '.fi' in domain:
|
|
|
107 |
return WhoisFi(domain, text)
|
|
0
|
108 |
else:
|
|
|
109 |
return WhoisEntry(domain, text)
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
class WhoisCom(WhoisEntry):
|
|
|
114 |
"""Whois parser for .com domains
|
|
|
115 |
"""
|
|
|
116 |
def __init__(self, domain, text):
|
|
|
117 |
if 'No match for "' in text:
|
|
|
118 |
raise PywhoisError(text)
|
|
|
119 |
else:
|
|
|
120 |
WhoisEntry.__init__(self, domain, text)
|
|
|
121 |
|
|
|
122 |
class WhoisNet(WhoisEntry):
|
|
|
123 |
"""Whois parser for .net domains
|
|
|
124 |
"""
|
|
|
125 |
def __init__(self, domain, text):
|
|
|
126 |
if 'No match for "' in text:
|
|
|
127 |
raise PywhoisError(text)
|
|
|
128 |
else:
|
|
|
129 |
WhoisEntry.__init__(self, domain, text)
|
|
|
130 |
|
|
|
131 |
class WhoisOrg(WhoisEntry):
|
|
|
132 |
"""Whois parser for .org domains
|
|
|
133 |
"""
|
|
|
134 |
def __init__(self, domain, text):
|
|
|
135 |
if text.strip() == 'NOT FOUND':
|
|
|
136 |
raise PywhoisError(text)
|
|
|
137 |
else:
|
|
|
138 |
WhoisEntry.__init__(self, domain, text)
|
|
|
139 |
|
|
|
140 |
class WhoisRu(WhoisEntry):
|
|
|
141 |
"""Whois parser for .ru domains
|
|
|
142 |
"""
|
|
|
143 |
regex = {
|
|
|
144 |
'domain_name': 'domain:\s*(.+)',
|
|
|
145 |
'registrar': 'registrar:\s*(.+)',
|
|
|
146 |
'creation_date': 'created:\s*(.+)',
|
|
|
147 |
'expiration_date': 'paid-till:\s*(.+)',
|
|
|
148 |
'name_servers': 'nserver:\s*(.+)', # list of name servers
|
|
|
149 |
'status': 'state:\s*(.+)', # list of statuses
|
|
|
150 |
'emails': '[\w.-]+@[\w.-]+\.[\w]{2,4}', # list of email addresses
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
def __init__(self, domain, text):
|
|
|
154 |
if text.strip() == 'No entries found':
|
|
|
155 |
raise PywhoisError(text)
|
|
|
156 |
else:
|
|
|
157 |
WhoisEntry.__init__(self, domain, text, self.regex)
|
|
|
158 |
|
|
|
159 |
class WhoisName(WhoisEntry):
|
|
|
160 |
"""Whois parser for .name domains
|
|
|
161 |
"""
|
|
|
162 |
regex = {
|
|
|
163 |
'domain_name_id': 'Domain Name ID:\s*(.+)',
|
|
|
164 |
'domain_name': 'Domain Name:\s*(.+)',
|
|
|
165 |
'registrar_id': 'Sponsoring Registrar ID:\s*(.+)',
|
|
|
166 |
'registrar': 'Sponsoring Registrar:\s*(.+)',
|
|
|
167 |
'registrant_id': 'Registrant ID:\s*(.+)',
|
|
|
168 |
'admin_id': 'Admin ID:\s*(.+)',
|
|
|
169 |
'technical_id': 'Tech ID:\s*(.+)',
|
|
|
170 |
'billing_id': 'Billing ID:\s*(.+)',
|
|
|
171 |
'creation_date': 'Created On:\s*(.+)',
|
|
|
172 |
'expiration_date': 'Expires On:\s*(.+)',
|
|
|
173 |
'updated_date': 'Updated On:\s*(.+)',
|
|
|
174 |
'name_server_ids': 'Name Server ID:\s*(.+)', # list of name server ids
|
|
|
175 |
'name_servers': 'Name Server:\s*(.+)', # list of name servers
|
|
|
176 |
'status': 'Domain Status:\s*(.+)', # list of statuses
|
|
|
177 |
}
|
|
|
178 |
def __init__(self, domain, text):
|
|
|
179 |
if 'No match.' in text:
|
|
|
180 |
raise PywhoisError(text)
|
|
|
181 |
else:
|
|
|
182 |
WhoisEntry.__init__(self, domain, text, self.regex)
|
|
|
183 |
|
|
|
184 |
class WhoisUs(WhoisEntry):
|
|
|
185 |
"""Whois parser for .us domains
|
|
|
186 |
"""
|
|
|
187 |
regex = {
|
|
|
188 |
'domain_name': 'Domain Name:\s*(.+)',
|
|
|
189 |
'domain__id': 'Domain ID:\s*(.+)',
|
|
|
190 |
'registrar': 'Sponsoring Registrar:\s*(.+)',
|
|
|
191 |
'registrar_id': 'Sponsoring Registrar IANA ID:\s*(.+)',
|
|
|
192 |
'registrar_url': 'Registrar URL \(registration services\):\s*(.+)',
|
|
|
193 |
'status': 'Domain Status:\s*(.+)', # list of statuses
|
|
|
194 |
'registrant_id': 'Registrant ID:\s*(.+)',
|
|
|
195 |
'registrant_name': 'Registrant Name:\s*(.+)',
|
|
|
196 |
'registrant_address1': 'Registrant Address1:\s*(.+)',
|
|
|
197 |
'registrant_address2': 'Registrant Address2:\s*(.+)',
|
|
|
198 |
'registrant_city': 'Registrant City:\s*(.+)',
|
|
|
199 |
'registrant_state_province': 'Registrant State/Province:\s*(.+)',
|
|
|
200 |
'registrant_postal_code': 'Registrant Postal Code:\s*(.+)',
|
|
|
201 |
'registrant_country': 'Registrant Country:\s*(.+)',
|
|
|
202 |
'registrant_country_code': 'Registrant Country Code:\s*(.+)',
|
|
|
203 |
'registrant_phone_number': 'Registrant Phone Number:\s*(.+)',
|
|
|
204 |
'registrant_email': 'Registrant Email:\s*(.+)',
|
|
|
205 |
'registrant_application_purpose': 'Registrant Application Purpose:\s*(.+)',
|
|
|
206 |
'registrant_nexus_category': 'Registrant Nexus Category:\s*(.+)',
|
|
|
207 |
'admin_id': 'Administrative Contact ID:\s*(.+)',
|
|
|
208 |
'admin_name': 'Administrative Contact Name:\s*(.+)',
|
|
|
209 |
'admin_address1': 'Administrative Contact Address1:\s*(.+)',
|
|
|
210 |
'admin_address2': 'Administrative Contact Address2:\s*(.+)',
|
|
|
211 |
'admin_city': 'Administrative Contact City:\s*(.+)',
|
|
|
212 |
'admin_state_province': 'Administrative Contact State/Province:\s*(.+)',
|
|
|
213 |
'admin_postal_code': 'Administrative Contact Postal Code:\s*(.+)',
|
|
|
214 |
'admin_country': 'Administrative Contact Country:\s*(.+)',
|
|
|
215 |
'admin_country_code': 'Administrative Contact Country Code:\s*(.+)',
|
|
|
216 |
'admin_phone_number': 'Administrative Contact Phone Number:\s*(.+)',
|
|
|
217 |
'admin_email': 'Administrative Contact Email:\s*(.+)',
|
|
|
218 |
'admin_application_purpose': 'Administrative Application Purpose:\s*(.+)',
|
|
|
219 |
'admin_nexus_category': 'Administrative Nexus Category:\s*(.+)',
|
|
|
220 |
'billing_id': 'Billing Contact ID:\s*(.+)',
|
|
|
221 |
'billing_name': 'Billing Contact Name:\s*(.+)',
|
|
|
222 |
'billing_address1': 'Billing Contact Address1:\s*(.+)',
|
|
|
223 |
'billing_address2': 'Billing Contact Address2:\s*(.+)',
|
|
|
224 |
'billing_city': 'Billing Contact City:\s*(.+)',
|
|
|
225 |
'billing_state_province': 'Billing Contact State/Province:\s*(.+)',
|
|
|
226 |
'billing_postal_code': 'Billing Contact Postal Code:\s*(.+)',
|
|
|
227 |
'billing_country': 'Billing Contact Country:\s*(.+)',
|
|
|
228 |
'billing_country_code': 'Billing Contact Country Code:\s*(.+)',
|
|
|
229 |
'billing_phone_number': 'Billing Contact Phone Number:\s*(.+)',
|
|
|
230 |
'billing_email': 'Billing Contact Email:\s*(.+)',
|
|
|
231 |
'billing_application_purpose': 'Billing Application Purpose:\s*(.+)',
|
|
|
232 |
'billing_nexus_category': 'Billing Nexus Category:\s*(.+)',
|
|
|
233 |
'tech_id': 'Technical Contact ID:\s*(.+)',
|
|
|
234 |
'tech_name': 'Technical Contact Name:\s*(.+)',
|
|
|
235 |
'tech_address1': 'Technical Contact Address1:\s*(.+)',
|
|
|
236 |
'tech_address2': 'Technical Contact Address2:\s*(.+)',
|
|
|
237 |
'tech_city': 'Technical Contact City:\s*(.+)',
|
|
|
238 |
'tech_state_province': 'Technical Contact State/Province:\s*(.+)',
|
|
|
239 |
'tech_postal_code': 'Technical Contact Postal Code:\s*(.+)',
|
|
|
240 |
'tech_country': 'Technical Contact Country:\s*(.+)',
|
|
|
241 |
'tech_country_code': 'Technical Contact Country Code:\s*(.+)',
|
|
|
242 |
'tech_phone_number': 'Technical Contact Phone Number:\s*(.+)',
|
|
|
243 |
'tech_email': 'Technical Contact Email:\s*(.+)',
|
|
|
244 |
'tech_application_purpose': 'Technical Application Purpose:\s*(.+)',
|
|
|
245 |
'tech_nexus_category': 'Technical Nexus Category:\s*(.+)',
|
|
|
246 |
'name_servers': 'Name Server:\s*(.+)', # list of name servers
|
|
|
247 |
'created_by_registrar': 'Created by Registrar:\s*(.+)',
|
|
|
248 |
'last_updated_by_registrar': 'Last Updated by Registrar:\s*(.+)',
|
|
|
249 |
'creation_date': 'Domain Registration Date:\s*(.+)',
|
|
|
250 |
'expiration_date': 'Domain Expiration Date:\s*(.+)',
|
|
|
251 |
'updated_date': 'Domain Last Updated Date:\s*(.+)',
|
|
|
252 |
}
|
|
|
253 |
def __init__(self, domain, text):
|
|
|
254 |
if 'Not found:' in text:
|
|
|
255 |
raise PywhoisError(text)
|
|
|
256 |
else:
|
|
|
257 |
WhoisEntry.__init__(self, domain, text, self.regex)
|
|
|
258 |
|
|
|
259 |
class WhoisMe(WhoisEntry):
|
|
|
260 |
"""Whois parser for .me domains
|
|
|
261 |
"""
|
|
|
262 |
regex = {
|
|
|
263 |
'domain_id': 'Domain ID:(.+)',
|
|
|
264 |
'domain_name': 'Domain Name:(.+)',
|
|
|
265 |
'creation_date': 'Domain Create Date:(.+)',
|
|
|
266 |
'updated_date': 'Domain Last Updated Date:(.+)',
|
|
|
267 |
'expiration_date': 'Domain Expiration Date:(.+)',
|
|
|
268 |
'transfer_date': 'Last Transferred Date:(.+)',
|
|
|
269 |
'trademark_name': 'Trademark Name:(.+)',
|
|
|
270 |
'trademark_country': 'Trademark Country:(.+)',
|
|
|
271 |
'trademark_number': 'Trademark Number:(.+)',
|
|
|
272 |
'trademark_application_date': 'Date Trademark Applied For:(.+)',
|
|
|
273 |
'trademark_registration_date': 'Date Trademark Registered:(.+)',
|
|
|
274 |
'registrar': 'Sponsoring Registrar:(.+)',
|
|
|
275 |
'created_by': 'Created by:(.+)',
|
|
|
276 |
'updated_by': 'Last Updated by Registrar:(.+)',
|
|
|
277 |
'status': 'Domain Status:(.+)', # list of statuses
|
|
|
278 |
'registrant_id': 'Registrant ID:(.+)',
|
|
|
279 |
'registrant_name': 'Registrant Name:(.+)',
|
|
|
280 |
'registrant_org': 'Registrant Organization:(.+)',
|
|
|
281 |
'registrant_address': 'Registrant Address:(.+)',
|
|
|
282 |
'registrant_address2': 'Registrant Address2:(.+)',
|
|
|
283 |
'registrant_address3': 'Registrant Address3:(.+)',
|
|
|
284 |
'registrant_city': 'Registrant City:(.+)',
|
|
|
285 |
'registrant_state_province': 'Registrant State/Province:(.+)',
|
|
|
286 |
'registrant_country': 'Registrant Country/Economy:(.+)',
|
|
|
287 |
'registrant_postal_code': 'Registrant Postal Code:(.+)',
|
|
|
288 |
'registrant_phone': 'Registrant Phone:(.+)',
|
|
|
289 |
'registrant_phone_ext': 'Registrant Phone Ext\.:(.+)',
|
|
|
290 |
'registrant_fax': 'Registrant FAX:(.+)',
|
|
|
291 |
'registrant_fax_ext': 'Registrant FAX Ext\.:(.+)',
|
|
|
292 |
'registrant_email': 'Registrant E-mail:(.+)',
|
|
|
293 |
'admin_id': 'Admin ID:(.+)',
|
|
|
294 |
'admin_name': 'Admin Name:(.+)',
|
|
|
295 |
'admin_org': 'Admin Organization:(.+)',
|
|
|
296 |
'admin_address': 'Admin Address:(.+)',
|
|
|
297 |
'admin_address2': 'Admin Address2:(.+)',
|
|
|
298 |
'admin_address3': 'Admin Address3:(.+)',
|
|
|
299 |
'admin_city': 'Admin City:(.+)',
|
|
|
300 |
'admin_state_province': 'Admin State/Province:(.+)',
|
|
|
301 |
'admin_country': 'Admin Country/Economy:(.+)',
|
|
|
302 |
'admin_postal_code': 'Admin Postal Code:(.+)',
|
|
|
303 |
'admin_phone': 'Admin Phone:(.+)',
|
|
|
304 |
'admin_phone_ext': 'Admin Phone Ext\.:(.+)',
|
|
|
305 |
'admin_fax': 'Admin FAX:(.+)',
|
|
|
306 |
'admin_fax_ext': 'Admin FAX Ext\.:(.+)',
|
|
|
307 |
'admin_email': 'Admin E-mail:(.+)',
|
|
|
308 |
'tech_id': 'Tech ID:(.+)',
|
|
|
309 |
'tech_name': 'Tech Name:(.+)',
|
|
|
310 |
'tech_org': 'Tech Organization:(.+)',
|
|
|
311 |
'tech_address': 'Tech Address:(.+)',
|
|
|
312 |
'tech_address2': 'Tech Address2:(.+)',
|
|
|
313 |
'tech_address3': 'Tech Address3:(.+)',
|
|
|
314 |
'tech_city': 'Tech City:(.+)',
|
|
|
315 |
'tech_state_province': 'Tech State/Province:(.+)',
|
|
|
316 |
'tech_country': 'Tech Country/Economy:(.+)',
|
|
|
317 |
'tech_postal_code': 'Tech Postal Code:(.+)',
|
|
|
318 |
'tech_phone': 'Tech Phone:(.+)',
|
|
|
319 |
'tech_phone_ext': 'Tech Phone Ext\.:(.+)',
|
|
|
320 |
'tech_fax': 'Tech FAX:(.+)',
|
|
|
321 |
'tech_fax_ext': 'Tech FAX Ext\.:(.+)',
|
|
|
322 |
'tech_email': 'Tech E-mail:(.+)',
|
|
|
323 |
'name_servers': 'Nameservers:(.+)', # list of name servers
|
|
|
324 |
}
|
|
|
325 |
def __init__(self, domain, text):
|
|
|
326 |
if 'NOT FOUND' in text:
|
|
|
327 |
raise PywhoisError(text)
|
|
|
328 |
else:
|
|
|
329 |
WhoisEntry.__init__(self, domain, text, self.regex)
|
|
|
330 |
|
|
|
331 |
class WhoisUk(WhoisEntry):
|
|
|
332 |
"""Whois parser for .uk domains
|
|
|
333 |
"""
|
|
|
334 |
regex = {
|
|
|
335 |
'domain_name': 'Domain name:\n\s*(.+)',
|
|
|
336 |
'registrar': 'Registrar:\n\s*(.+)',
|
|
|
337 |
'registrar_url': 'URL:\s*(.+)',
|
|
|
338 |
'status': 'Registration status:\n\s*(.+)', # list of statuses
|
|
|
339 |
'registrant_name': 'Registrant:\n\s*(.+)',
|
|
|
340 |
'creation_date': 'Registered on:\s*(.+)',
|
|
|
341 |
'expiration_date': 'Renewal date:\s*(.+)',
|
|
|
342 |
'updated_date': 'Last updated:\s*(.+)',
|
|
|
343 |
}
|
|
|
344 |
def __init__(self, domain, text):
|
|
|
345 |
if 'Not found:' in text:
|
|
|
346 |
raise PywhoisError(text)
|
|
|
347 |
else:
|
|
|
348 |
WhoisEntry.__init__(self, domain, text, self.regex)
|
|
2
|
349 |
|
|
|
350 |
class WhoisFi(WhoisEntry):
|
|
|
351 |
"""Whois parser for .fi domains
|
|
|
352 |
"""
|
|
|
353 |
regex = {
|
|
|
354 |
'domain_name': 'domain:\s*([\S]+)',
|
|
|
355 |
'registrant_name': 'descr:\s*([\S\ ]+)',
|
|
|
356 |
'registrant_address': 'address:\s*([\S\ ]+)',
|
|
|
357 |
'registrant_phone': 'phone:\s*([\S\ ]+)',
|
|
|
358 |
'status': 'status:\s*([\S]+)', # list of statuses
|
|
|
359 |
'creation_date': 'created:\s*([\S]+)',
|
|
|
360 |
'updated_date': 'modified:\s*([\S]+)',
|
|
|
361 |
'expiration_date': 'expires:\s*([\S]+)',
|
|
|
362 |
'name_servers': 'nserver:\s*([\S]+) \[(\S+)\]', # list of name servers
|
|
|
363 |
'dnssec': 'dnssec:\s*([\S]+)', # list of name servers
|
|
|
364 |
}
|
|
|
365 |
def __init__(self, domain, text):
|
|
|
366 |
if 'Not found:' in text:
|
|
|
367 |
raise PywhoisError(text)
|
|
|
368 |
else:
|
|
|
369 |
WhoisEntry.__init__(self, domain, text, self.regex)
|