whois/whois.py
changeset 105 2228d503a1f0
parent 97 44522cd37b07
child 106 40e0392ef844
equal deleted inserted replaced
104:67a8d95375b6 105:2228d503a1f0
    89         """Perform initial lookup with TLD whois server
    89         """Perform initial lookup with TLD whois server
    90         then, if the quick flag is false, search that result
    90         then, if the quick flag is false, search that result
    91         for the region-specifc whois server and do a lookup
    91         for the region-specifc whois server and do a lookup
    92         there for contact details
    92         there for contact details
    93         """
    93         """
       
    94         response = b''
    94         try:
    95         try:
    95             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    96             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    96             s.settimeout(10)
    97             s.settimeout(10)
    97             s.connect((hostname, 43))
    98             s.connect((hostname, 43))
    98             # end takes bytes as an input
    99 
    99             queryBytes = None
       
   100             try:
   100             try:
   101                 query = query.decode('utf-8')
   101                 query = query.decode('utf-8')
   102             except UnicodeEncodeError:
   102             except UnicodeEncodeError:
   103                 pass  # Already Unicode (python2's error)
   103                 pass  # Already Unicode (python2's error)
   104             except AttributeError:
   104             except AttributeError:
   105                 pass  # Already Unicode (python3's error)
   105                 pass  # Already Unicode (python3's error)
   106 
   106 
   107             if hostname == NICClient.DENICHOST:
   107             if hostname == NICClient.DENICHOST:
   108                 queryBytes = "-T dn,ace -C UTF-8 " + query
   108                 query_bytes = "-T dn,ace -C UTF-8 " + query
   109             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   109             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   110                 queryBytes = '=' + query
   110                 query_bytes = '=' + query
   111             else:
   111             else:
   112                 queryBytes = query
   112                 query_bytes = query
   113             s.send((queryBytes + "\r\n").encode('idna'))
   113             s.send((query_bytes + "\r\n").encode('idna'))
   114             # recv returns bytes
   114             # recv returns bytes
   115             response = b''
       
   116             while True:
   115             while True:
   117                 d = s.recv(4096)
   116                 d = s.recv(4096)
   118                 response += d
   117                 response += d
   119                 if not d:
   118                 if not d:
   120                     break
   119                     break
   121             s.close()
   120             s.close()
   122         except socket.error as socketerror:
   121         except socket.error:
   123             print('Socket Error:', socketerror)
   122             print('Socket Error:', socketerror)
   124             return ''
       
   125         else:
       
   126             nhost = None
       
   127             response = response.decode('utf-8', errors='replace')
       
   128             if 'with "=xxx"' in response:
       
   129                 return self.whois(query, hostname, flags, True)
       
   130             if flags & NICClient.WHOIS_RECURSE and nhost is None:
       
   131                 nhost = self.findwhois_server(response, hostname, query)
       
   132             if nhost is not None:
       
   133                 response += self.whois(query, nhost, 0)
       
   134             return response
   123             return response
   135 
   124 
       
   125         nhost = None
       
   126         response = response.decode('utf-8', errors='replace')
       
   127         if 'with "=xxx"' in response:
       
   128             return self.whois(query, hostname, flags, True)
       
   129         if flags & NICClient.WHOIS_RECURSE and nhost is None:
       
   130             nhost = self.findwhois_server(response, hostname, query)
       
   131         if nhost is not None:
       
   132             response += self.whois(query, nhost, 0)
       
   133         return response
   136 
   134 
   137     def choose_server(self, domain):
   135     def choose_server(self, domain):
   138         """Choose initial lookup NIC host"""
   136         """Choose initial lookup NIC host"""
   139         try:
   137         try:
   140             domain = domain.encode('idna').decode('utf-8')
   138             domain = domain.encode('idna').decode('utf-8')