added support for native client
authorRichard Penman
Sat, 05 Sep 2015 23:01:44 +0800
changeset 60 7801a420f679
parent 59 f1e5214a1b16
child 61 c391985a797b
child 62 fc06c601b875
added support for native client
whois/__init__.py
whois/whois.py
--- a/whois/__init__.py	Tue Sep 01 12:06:17 2015 +0800
+++ b/whois/__init__.py	Sat Sep 05 23:01:44 2015 +0800
@@ -7,23 +7,19 @@
 from whois import NICClient
 
 
-def whois(url, experimental=False):
+def whois(url, command=False):
     # clean domain to expose netloc
     ip_match = re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", url)
     if ip_match:
         domain = url
     else:
         domain = extract_domain(url)
-    if not experimental:
-        try:
-            # try native whois command first
-            r = subprocess.Popen(['whois', domain], stdout=subprocess.PIPE)
-            text = r.stdout.read()
-        except OSError:
-            # try experimental client
-            nic_client = NICClient()
-            text = nic_client.whois_lookup(None, domain, 0)
+    if command:
+        # try native whois command
+        r = subprocess.Popen(['whois', domain], stdout=subprocess.PIPE)
+        text = r.stdout.read()
     else:
+        # try builtin client
         nic_client = NICClient()
         text = nic_client.whois_lookup(None, domain, 0)
     return WhoisEntry.load(domain, text)
@@ -34,10 +30,8 @@
 
     >>> extract_domain('http://www.google.com.au/tos.html')
     'google.com.au'
-    >>> extract_domain('http://blog.webscraping.com')
+    >>> extract_domain('www.webscraping.com')
     'webscraping.com'
-    >>> extract_domain('www.bbc.co.uk')
-    'bbc.co.uk'
     >>> extract_domain('198.252.206.140')
     'stackoverflow.com'
     >>> extract_domain('102.112.2O7.net')
@@ -73,6 +67,6 @@
     try:
         url = sys.argv[1]
     except IndexError:
-        print('Usage: %s url' % sys.argv[0])
+        print 'Usage: %s url' % sys.argv[0]
     else:
-        print(whois(url))
+        print whois(url)
--- a/whois/whois.py	Tue Sep 01 12:06:17 2015 +0800
+++ b/whois/whois.py	Sat Sep 05 23:01:44 2015 +0800
@@ -24,21 +24,14 @@
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
-  Last edited by:  $Author$
-              on:  $DateTime$
-        Revision:  $Revision$
-              Id:  $Id$
-          Author:  Chris Wolf
 """
 import sys
 import socket
 import optparse
-# import pdb
 
 
 def enforce_ascii(a):
     if isinstance(a, str) or isinstance(a, unicode):
-        # return a.encode('ascii', 'replace')
         r = ""
         for i in a:
             if ord(i) >= 128:
@@ -84,28 +77,22 @@
         """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
         nhost = None
         parts_index = 1
         start = buf.find(NICClient.WHOIS_SERVER_ID)
-        # print 'start', start
         if (start == -1):
             start = buf.find(NICClient.WHOIS_ORG_SERVER_ID)
             parts_index = 2
 
         if (start > -1):
             end = buf[start:].find('\n')
-            # print 'end:', end
             whois_line = buf[start:end+start]
-            # 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 nhost.count('/') > 0:
                 nhost = None
-            # print 'nhost:',nhost
         elif (hostname == NICClient.ANICHOST):
             for nichost in NICClient.ip_whois:
                 if (buf.find(nichost) != -1):
@@ -119,9 +106,6 @@
         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()
         try:
           s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
           s.settimeout(2)
@@ -132,16 +116,15 @@
           if type(query) is not unicode:
               query = query.decode('utf-8')
 
-          if (hostname == NICClient.DENICHOST):
-              # print 'the domain is in NIC DENIC'
+          if hostname == NICClient.DENICHOST:
               queryBytes = ("-T dn,ace -C UTF-8 " + query + "\r\n").encode('idna')
-              # print 'queryBytes:', queryBytes
+          elif hostname == 'com.whois-servers.net':
+              queryBytes = ('=' + query + "\r\n").encode('idna')
           else:
               queryBytes = (query + "\r\n").encode('idna')
           s.send(queryBytes)
           """recv returns bytes
           """
-          # print s
           response = b''
           while True:
               d = s.recv(4096)
@@ -150,20 +133,13 @@
                   break
           s.close()
         except socket.error as socketerror:
-          print("Error: ", socketerror)
-        # pdb.set_trace()
+          print "Error: ", socketerror
         nhost = None
-        # print 'response', response
         response = enforce_ascii(response)
         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 is not None):
-            # print 'inside second if'
             response += self.whois(query, nhost, 0)
-            # print 'response', response
-        # print 'returning whois response'
         return response.decode()
 
     def choose_server(self, domain):
@@ -186,11 +162,9 @@
         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'
         nichost = None
-        # pdb.set_trace()
         # whoud happen when this function is called by other than main
-        if (options is None):
+        if options is None:
             options = {}
 
         if (('whoishost' not in options or options['whoishost'] is None)
@@ -200,13 +174,13 @@
             if (not (flags & NICClient.WHOIS_QUICK)):
                 flags |= NICClient.WHOIS_RECURSE
 
-        if ('country' in options and options['country'] is not None):
+        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):
+        elif self.use_qnichost:
             nichost = self.choose_server(query_arg)
             if (nichost is not None):
                 result = self.whois(query_arg, nichost, flags)
@@ -214,7 +188,6 @@
                 result = ''
         else:
             result = self.whois(query_arg, options['whoishost'], flags)
-        # print 'whois_lookup finished'
         return result
 
 
@@ -285,4 +258,4 @@
     (options, args) = parse_command_line(sys.argv)
     if (options.b_quicklookup is True):
         flags = flags | NICClient.WHOIS_QUICK
-    print(nic_client.whois_lookup(options.__dict__, args[1], flags))
+    print nic_client.whois_lookup(options.__dict__, args[1], flags)