--- a/whois/whois.py Tue Aug 12 15:57:28 2014 +0300
+++ b/whois/whois.py Thu Aug 14 18:25:14 2014 +0300
@@ -33,7 +33,8 @@
import sys
import socket
import optparse
-#import pdb
+# import pdb
+
def enforce_ascii(a):
if isinstance(a, str) or isinstance(a, unicode):
@@ -48,94 +49,94 @@
else:
return a
-class NICClient(object) :
+
+class NICClient(object):
- ABUSEHOST = "whois.abuse.net"
- NICHOST = "whois.crsnic.net"
- INICHOST = "whois.networksolutions.com"
- DNICHOST = "whois.nic.mil"
- GNICHOST = "whois.nic.gov"
- ANICHOST = "whois.arin.net"
- LNICHOST = "whois.lacnic.net"
- RNICHOST = "whois.ripe.net"
- PNICHOST = "whois.apnic.net"
- MNICHOST = "whois.ra.net"
- QNICHOST_TAIL = ".whois-servers.net"
- SNICHOST = "whois.6bone.net"
- BNICHOST = "whois.registro.br"
- NORIDHOST = "whois.norid.no"
- IANAHOST = "whois.iana.org"
- DENICHOST = "de.whois-servers.net"
- DEFAULT_PORT = "nicname"
- WHOIS_SERVER_ID = "Whois Server:"
+ ABUSEHOST = "whois.abuse.net"
+ NICHOST = "whois.crsnic.net"
+ INICHOST = "whois.networksolutions.com"
+ DNICHOST = "whois.nic.mil"
+ GNICHOST = "whois.nic.gov"
+ ANICHOST = "whois.arin.net"
+ LNICHOST = "whois.lacnic.net"
+ RNICHOST = "whois.ripe.net"
+ PNICHOST = "whois.apnic.net"
+ MNICHOST = "whois.ra.net"
+ QNICHOST_TAIL = ".whois-servers.net"
+ SNICHOST = "whois.6bone.net"
+ BNICHOST = "whois.registro.br"
+ NORIDHOST = "whois.norid.no"
+ IANAHOST = "whois.iana.org"
+ DENICHOST = "de.whois-servers.net"
+ DEFAULT_PORT = "nicname"
+ WHOIS_SERVER_ID = "Whois Server:"
WHOIS_ORG_SERVER_ID = "Registrant Street1:Whois Server:"
-
- WHOIS_RECURSE = 0x01
- WHOIS_QUICK = 0x02
+ WHOIS_RECURSE = 0x01
+ WHOIS_QUICK = 0x02
- ip_whois = [ LNICHOST, RNICHOST, PNICHOST, BNICHOST ]
+ ip_whois = [LNICHOST, RNICHOST, PNICHOST, BNICHOST]
- def __init__(self) :
+ def __init__(self):
self.use_qnichost = False
-
+
def findwhois_server(self, buf, hostname):
"""Search the initial TLD lookup results for the regional-specifc
whois server for getting contact details.
"""
- #print 'finding whois server'
- #print 'parameters:', buf, 'hostname', hostname
+ # print 'finding whois server'
+ # print 'parameters:', buf, 'hostname', hostname
nhost = None
parts_index = 1
start = buf.find(NICClient.WHOIS_SERVER_ID)
- #print 'start', start
+ # print 'start', start
if (start == -1):
start = buf.find(NICClient.WHOIS_ORG_SERVER_ID)
parts_index = 2
-
- if (start > -1):
+
+ if (start > -1):
end = buf[start:].find('\n')
- #print 'end:', end
+ # print 'end:', end
whois_line = buf[start:end+start]
- #print 'whois_line', whois_line
+ # print 'whois_line', whois_line
nhost = whois_line.split(NICClient.WHOIS_SERVER_ID+' ').pop()
nhost = nhost.split('http://').pop()
- #if the whois address is domain.tld/something then
- #s.connect((hostname, 43)) does not work
+ # if the whois address is domain.tld/something then
+ # s.connect((hostname, 43)) does not work
if nhost.count('/') > 0:
nhost = None
- #print 'nhost:',nhost
+ # print 'nhost:',nhost
elif (hostname == NICClient.ANICHOST):
for nichost in NICClient.ip_whois:
if (buf.find(nichost) != -1):
nhost = nichost
break
return nhost
-
+
def whois(self, query, hostname, flags):
"""Perform initial lookup with TLD whois server
then, if the quick flag is false, search that result
for the region-specifc whois server and do a lookup
there for contact details
"""
- #print 'Performing the whois'
- #print 'parameters given:', query, hostname, flags
- #pdb.set_trace()
+ # print 'Performing the whois'
+ # print 'parameters given:', query, hostname, flags
+ # pdb.set_trace()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, 43))
"""send takes bytes as an input
"""
queryBytes = None
if (hostname == NICClient.DENICHOST):
- #print 'the domain is in NIC DENIC'
+ # print 'the domain is in NIC DENIC'
queryBytes = ("-T dn,ace -C UTF-8 " + query + "\r\n").encode()
- #print 'queryBytes:', queryBytes
+ # print 'queryBytes:', queryBytes
else:
- queryBytes = (query + "\r\n").encode()
+ queryBytes = (query + "\r\n").encode()
s.send(queryBytes)
"""recv returns bytes
"""
- #print s
+ # print s
response = b''
while True:
d = s.recv(4096)
@@ -143,21 +144,21 @@
if not d:
break
s.close()
- #pdb.set_trace()
+ # pdb.set_trace()
nhost = None
- #print 'response', response
+ # print 'response', response
response = enforce_ascii(response)
- if (flags & NICClient.WHOIS_RECURSE and nhost == None):
- #print 'Inside first if'
+ if (flags & NICClient.WHOIS_RECURSE and nhost is None):
+ # print 'Inside first if'
nhost = self.findwhois_server(response.decode(), hostname)
- #print 'nhost is:', nhost
- if (nhost != None):
- #print 'inside second if'
+ # print 'nhost is:', nhost
+ if (nhost is not None):
+ # print 'inside second if'
response += self.whois(query, nhost, 0)
- #print 'response', response
- #print 'returning whois response'
+ # print 'response', response
+ # print 'returning whois response'
return response.decode()
-
+
def choose_server(self, domain):
"""Choose initial lookup NIC host"""
if (domain.endswith("-NORID")):
@@ -168,48 +169,52 @@
tld = domain[pos+1:]
if (tld[0].isdigit()):
return NICClient.ANICHOST
-
+
return tld + NICClient.QNICHOST_TAIL
-
+
def whois_lookup(self, options, query_arg, flags):
"""Main entry point: Perform initial lookup on TLD whois server,
or other server to get region-specific whois server, then if quick
flag is false, perform a second lookup on the region-specific
server for contact records"""
- #print 'whois_lookup'
+ # print 'whois_lookup'
nichost = None
- #pdb.set_trace()
- # this would be the case when this function is called by other than main
- if (options == None):
+ # pdb.set_trace()
+ # whoud happen when this function is called by other than main
+ if (options is None):
options = {}
-
- if ( (not 'whoishost' in options or options['whoishost'] == None)
- and (not 'country' in options or options['country'] == None)):
+
+ if (('whoishost' not in options or options['whoishost'] is None)
+ and ('country' not in options or options['country'] is None)):
self.use_qnichost = True
options['whoishost'] = NICClient.NICHOST
- if ( not (flags & NICClient.WHOIS_QUICK)):
+ if (not (flags & NICClient.WHOIS_QUICK)):
flags |= NICClient.WHOIS_RECURSE
-
- if ('country' in options and options['country'] != None):
- result = self.whois(query_arg, options['country'] + NICClient.QNICHOST_TAIL, flags)
+
+ if ('country' in options and options['country'] is not None):
+ result = self.whois(
+ query_arg,
+ options['country'] + NICClient.QNICHOST_TAIL,
+ flags
+ )
elif (self.use_qnichost):
nichost = self.choose_server(query_arg)
- if (nichost != None):
+ if (nichost is not None):
result = self.whois(query_arg, nichost, flags)
else:
result = self.whois(query_arg, options['whoishost'], flags)
- #print 'whois_lookup finished'
+ # print 'whois_lookup finished'
return result
-#---- END OF NICClient class def ---------------------
-
+
+
def parse_command_line(argv):
"""Options handling mostly follows the UNIX whois(1) man page, except
long-form options can also be used.
"""
flags = 0
-
+
usage = "usage: %prog [options] name"
-
+
parser = optparse.OptionParser(add_help_option=False, usage=usage)
parser.add_option("-a", "--arin", action="store_const",
const=NICClient.ANICHOST, dest="whoishost",
@@ -231,7 +236,7 @@
help="Lookup using host " + NICClient.GNICHOST)
parser.add_option("-h", "--host", action="store",
type="string", dest="whoishost",
- help="Lookup using specified whois host")
+ help="Lookup using specified whois host")
parser.add_option("-i", "--nws", action="store_const",
const=NICClient.INICHOST, dest="whoishost",
help="Lookup using host " + NICClient.INICHOST)
@@ -248,8 +253,8 @@
type="int", dest="port",
help="Lookup using specified tcp port")
parser.add_option("-Q", "--quick", action="store_true",
- dest="b_quicklookup",
- help="Perform quick lookup")
+ dest="b_quicklookup",
+ help="Perform quick lookup")
parser.add_option("-r", "--ripe", action="store_const",
const=NICClient.RNICHOST, dest="whoishost",
help="Lookup using host " + NICClient.RNICHOST)
@@ -261,13 +266,12 @@
help="Lookup using host " + NICClient.SNICHOST)
parser.add_option("-?", "--help", action="help")
-
return parser.parse_args(argv)
-
+
if __name__ == "__main__":
flags = 0
nic_client = NICClient()
(options, args) = parse_command_line(sys.argv)
if (options.b_quicklookup is True):
- flags = flags|NICClient.WHOIS_QUICK
+ flags = flags | NICClient.WHOIS_QUICK
print(nic_client.whois_lookup(options.__dict__, args[1], flags))