whois/whois.py
changeset 80 fa9650e9ec23
parent 65 50b5966b5566
child 81 359baebcf0e8
equal deleted inserted replaced
65:50b5966b5566 80:fa9650e9ec23
    27 import re
    27 import re
    28 import sys
    28 import sys
    29 import socket
    29 import socket
    30 import optparse
    30 import optparse
    31 
    31 
    32 
       
    33 def enforce_ascii(a):
       
    34     if isinstance(a, str) or isinstance(a, unicode):
       
    35         r = ""
       
    36         for i in a:
       
    37             if ord(i) >= 128:
       
    38                 r += "?"
       
    39             else:
       
    40                 r += i
       
    41         return r
       
    42     else:
       
    43         return a
       
    44 
    32 
    45 
    33 
    46 class NICClient(object):
    34 class NICClient(object):
    47 
    35 
    48     ABUSEHOST = "whois.abuse.net"
    36     ABUSEHOST = "whois.abuse.net"
   123         except socket.error as socketerror:
   111         except socket.error as socketerror:
   124             print 'Socket Error:', socketerror
   112             print 'Socket Error:', socketerror
   125             return ''
   113             return ''
   126         else:
   114         else:
   127             nhost = None
   115             nhost = None
   128             response = enforce_ascii(response)
   116             response = response.decode('utf-8')
   129             if 'with "=xxx"' in response:
   117             if 'with "=xxx"' in response:
   130                 return self.whois(query, hostname, flags, True)
   118                 return self.whois(query, hostname, flags, True)
   131             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   119             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   132                 nhost = self.findwhois_server(response.decode(), hostname, query)
   120                 nhost = self.findwhois_server(response, hostname, query)
   133             if nhost is not None:
   121             if nhost is not None:
   134                 response += self.whois(query, nhost, 0)
   122                 response += self.whois(query, nhost, 0)
   135             return response.decode()
   123             return response
   136 
   124 
   137     def choose_server(self, domain):
   125     def choose_server(self, domain):
   138         """Choose initial lookup NIC host"""
   126         """Choose initial lookup NIC host"""
   139         if type(domain) is not unicode:
   127         if type(domain) is not unicode:
   140             domain = domain.decode('utf-8').encode('idna')
   128             domain = domain.decode('utf-8').encode('idna')