whois/whois.py
changeset 97 44522cd37b07
parent 96 4177eba88c22
child 105 2228d503a1f0
equal deleted inserted replaced
96:4177eba88c22 97:44522cd37b07
    95             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    95             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    96             s.settimeout(10)
    96             s.settimeout(10)
    97             s.connect((hostname, 43))
    97             s.connect((hostname, 43))
    98             # end takes bytes as an input
    98             # end takes bytes as an input
    99             queryBytes = None
    99             queryBytes = None
   100             if type(query) is not str:
   100             try:
   101                 query = query.decode('utf-8')
   101                 query = query.decode('utf-8')
       
   102             except UnicodeEncodeError:
       
   103                 pass  # Already Unicode (python2's error)
       
   104             except AttributeError:
       
   105                 pass  # Already Unicode (python3's error)
   102 
   106 
   103             if hostname == NICClient.DENICHOST:
   107             if hostname == NICClient.DENICHOST:
   104                 queryBytes = "-T dn,ace -C UTF-8 " + query
   108                 queryBytes = "-T dn,ace -C UTF-8 " + query
   105             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   109             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   106                 queryBytes = '=' + query
   110                 queryBytes = '=' + query
   130             return response
   134             return response
   131 
   135 
   132 
   136 
   133     def choose_server(self, domain):
   137     def choose_server(self, domain):
   134         """Choose initial lookup NIC host"""
   138         """Choose initial lookup NIC host"""
   135         if type(domain) is not str:
   139         try:
       
   140             domain = domain.encode('idna').decode('utf-8')
       
   141         except TypeError:
   136             domain = domain.decode('utf-8').encode('idna').decode('utf-8')
   142             domain = domain.decode('utf-8').encode('idna').decode('utf-8')
   137         if domain.endswith("-NORID"):
   143         if domain.endswith("-NORID"):
   138             return NICClient.NORIDHOST
   144             return NICClient.NORIDHOST
   139         pos = domain.rfind('.')
   145         domain = domain.split('.')
   140         if pos == -1:
   146         if len(domain) < 2:
   141             return None
   147             return None
   142         tld = domain[pos+1:]
   148         tld = domain[-1]
   143         if tld[0].isdigit():
   149         if tld[0].isdigit():
   144             return NICClient.ANICHOST
   150             return NICClient.ANICHOST
   145         return tld + NICClient.QNICHOST_TAIL
   151         return tld + NICClient.QNICHOST_TAIL
   146 
   152 
   147     def whois_lookup(self, options, query_arg, flags):
   153     def whois_lookup(self, options, query_arg, flags):