whois/whois.py
branchpython3
changeset 70 1fe2c20adeba
parent 65 50b5966b5566
child 71 b181f795cc0d
equal deleted inserted replaced
65:50b5966b5566 70:1fe2c20adeba
    22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    25 THE SOFTWARE.
    25 THE SOFTWARE.
    26 """
    26 """
       
    27 from __future__ import print_function
       
    28 from __future__ import unicode_literals
       
    29 from __future__ import division
       
    30 from __future__ import absolute_import
       
    31 from future import standard_library
       
    32 standard_library.install_aliases()
       
    33 from builtins import *
       
    34 from builtins import object
    27 import re
    35 import re
    28 import sys
    36 import sys
    29 import socket
    37 import socket
    30 import optparse
    38 import optparse
    31 
    39 
    32 
    40 
    33 def enforce_ascii(a):
    41 def enforce_ascii(a):
    34     if isinstance(a, str) or isinstance(a, unicode):
    42     return a if isinstance(a, bytes) else bytes([63 if ord(c) > 127 else ord(c) for c in a])
    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 
       
    45 
    43 
    46 class NICClient(object):
    44 class NICClient(object):
    47 
    45 
    48     ABUSEHOST = "whois.abuse.net"
    46     ABUSEHOST = "whois.abuse.net"
    49     NICHOST = "whois.crsnic.net"
    47     NICHOST = "whois.crsnic.net"
   100             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    98             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   101             s.settimeout(10)
    99             s.settimeout(10)
   102             s.connect((hostname, 43))
   100             s.connect((hostname, 43))
   103             # end takes bytes as an input
   101             # end takes bytes as an input
   104             queryBytes = None
   102             queryBytes = None
   105             if type(query) is not unicode:
   103             if type(query) is not str:
   106                 query = query.decode('utf-8')
   104                 query = query.decode('utf-8')
   107 
   105 
   108             if hostname == NICClient.DENICHOST:
   106             if hostname == NICClient.DENICHOST:
   109                 queryBytes = "-T dn,ace -C UTF-8 " + query
   107                 queryBytes = "-T dn,ace -C UTF-8 " + query
   110             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   108             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   119                 response += d
   117                 response += d
   120                 if not d:
   118                 if not d:
   121                     break
   119                     break
   122             s.close()
   120             s.close()
   123         except socket.error as socketerror:
   121         except socket.error as socketerror:
   124             print 'Socket Error:', socketerror
   122             print('Socket Error:', socketerror)
   125             return ''
   123             return ''
   126         else:
   124         else:
   127             nhost = None
   125             nhost = None
   128             response = enforce_ascii(response)
   126             response = enforce_ascii(response)
   129             if 'with "=xxx"' in response:
   127             if b'with "=xxx"' in response:
   130                 return self.whois(query, hostname, flags, True)
   128                 return self.whois(query, hostname, flags, True)
   131             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   129             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   132                 nhost = self.findwhois_server(response.decode(), hostname, query)
   130                 nhost = self.findwhois_server(response.decode(), hostname, query)
   133             if nhost is not None:
   131             if nhost is not None:
   134                 response += self.whois(query, nhost, 0)
   132                 response += self.whois(query, nhost, 0)
   135             return response.decode()
   133             return response.decode()
   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         if type(domain) is not unicode:
   137         if type(domain) is not str:
   140             domain = domain.decode('utf-8').encode('idna')
   138             domain = domain.decode('utf-8').encode('idna').decode('utf-8')
   141         if domain.endswith("-NORID"):
   139         if domain.endswith("-NORID"):
   142             return NICClient.NORIDHOST
   140             return NICClient.NORIDHOST
   143         pos = domain.rfind('.')
   141         pos = domain.rfind('.')
   144         if pos == -1:
   142         if pos == -1:
   145             return None
   143             return None
   248     flags = 0
   246     flags = 0
   249     nic_client = NICClient()
   247     nic_client = NICClient()
   250     options, args = parse_command_line(sys.argv)
   248     options, args = parse_command_line(sys.argv)
   251     if options.b_quicklookup:
   249     if options.b_quicklookup:
   252         flags = flags | NICClient.WHOIS_QUICK
   250         flags = flags | NICClient.WHOIS_QUICK
   253     print nic_client.whois_lookup(options.__dict__, args[1], flags)
   251     print(nic_client.whois_lookup(options.__dict__, args[1], flags))