whois/whois.py
branchpython3
changeset 71 b181f795cc0d
parent 70 1fe2c20adeba
child 73 644d81a7995b
equal deleted inserted replaced
70:1fe2c20adeba 71:b181f795cc0d
    37 import socket
    37 import socket
    38 import optparse
    38 import optparse
    39 
    39 
    40 
    40 
    41 def enforce_ascii(a):
    41 def enforce_ascii(a):
    42     return a if isinstance(a, bytes) else bytes([63 if ord(c) > 127 else ord(c) for c in a])
    42     return a if isinstance(a, bytes) else bytes([63 if ord(c) > 127 else ord(c) for c in a]).decode('ascii')
    43 
    43 
    44 class NICClient(object):
    44 class NICClient(object):
    45 
    45 
    46     ABUSEHOST = "whois.abuse.net"
    46     ABUSEHOST = "whois.abuse.net"
    47     NICHOST = "whois.crsnic.net"
    47     NICHOST = "whois.crsnic.net"
   127             if b'with "=xxx"' in response:
   127             if b'with "=xxx"' in response:
   128                 return self.whois(query, hostname, flags, True)
   128                 return self.whois(query, hostname, flags, True)
   129             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   129             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   130                 nhost = self.findwhois_server(response.decode(), hostname, query)
   130                 nhost = self.findwhois_server(response.decode(), hostname, query)
   131             if nhost is not None:
   131             if nhost is not None:
   132                 response += self.whois(query, nhost, 0)
   132                 response += self.whois(query, nhost, 0).encode('utf-8')
   133             return response.decode()
   133             return response.decode()
   134 
   134 
   135     def choose_server(self, domain):
   135     def choose_server(self, domain):
   136         """Choose initial lookup NIC host"""
   136         """Choose initial lookup NIC host"""
       
   137         print('domain:', domain)
   137         if type(domain) is not str:
   138         if type(domain) is not str:
   138             domain = domain.decode('utf-8').encode('idna').decode('utf-8')
   139             domain = domain.decode('utf-8').encode('idna').decode('utf-8')
   139         if domain.endswith("-NORID"):
   140         if domain.endswith("-NORID"):
   140             return NICClient.NORIDHOST
   141             return NICClient.NORIDHOST
   141         pos = domain.rfind('.')
   142         pos = domain.rfind('.')
   169                 options['country'] + NICClient.QNICHOST_TAIL,
   170                 options['country'] + NICClient.QNICHOST_TAIL,
   170                 flags
   171                 flags
   171             )
   172             )
   172         elif self.use_qnichost:
   173         elif self.use_qnichost:
   173             nichost = self.choose_server(query_arg)
   174             nichost = self.choose_server(query_arg)
       
   175             print('nichost:', nichost)
   174             if nichost is not None:
   176             if nichost is not None:
   175                 result = self.whois(query_arg, nichost, flags)
   177                 result = self.whois(query_arg, nichost, flags)
   176             else:
   178             else:
   177                 result = ''
   179                 result = ''
   178         else:
   180         else: