whois/whois.py
changeset 81 359baebcf0e8
parent 80 fa9650e9ec23
parent 73 644d81a7995b
child 82 fc40924ac59b
equal deleted inserted replaced
80:fa9650e9ec23 81:359baebcf0e8
    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 
       
    32 
    39 
    33 
    40 
    34 class NICClient(object):
    41 class NICClient(object):
    35 
    42 
    36     ABUSEHOST = "whois.abuse.net"
    43     ABUSEHOST = "whois.abuse.net"
    88             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    95             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    89             s.settimeout(10)
    96             s.settimeout(10)
    90             s.connect((hostname, 43))
    97             s.connect((hostname, 43))
    91             # end takes bytes as an input
    98             # end takes bytes as an input
    92             queryBytes = None
    99             queryBytes = None
    93             if type(query) is not unicode:
   100             if type(query) is not str:
    94                 query = query.decode('utf-8')
   101                 query = query.decode('utf-8')
    95 
   102 
    96             if hostname == NICClient.DENICHOST:
   103             if hostname == NICClient.DENICHOST:
    97                 queryBytes = "-T dn,ace -C UTF-8 " + query
   104                 queryBytes = "-T dn,ace -C UTF-8 " + query
    98             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   105             elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
   107                 response += d
   114                 response += d
   108                 if not d:
   115                 if not d:
   109                     break
   116                     break
   110             s.close()
   117             s.close()
   111         except socket.error as socketerror:
   118         except socket.error as socketerror:
   112             print 'Socket Error:', socketerror
   119             print('Socket Error:', socketerror)
   113             return ''
   120             return ''
   114         else:
   121         else:
   115             nhost = None
   122             nhost = None
   116             response = response.decode('utf-8')
   123             response = response.decode('utf-8')
   117             if 'with "=xxx"' in response:
   124             if b'with "=xxx"' in response:
   118                 return self.whois(query, hostname, flags, True)
   125                 return self.whois(query, hostname, flags, True)
   119             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   126             if flags & NICClient.WHOIS_RECURSE and nhost is None:
   120                 nhost = self.findwhois_server(response, hostname, query)
   127                 nhost = self.findwhois_server(response, hostname, query)
   121             if nhost is not None:
   128             if nhost is not None:
   122                 response += self.whois(query, nhost, 0)
   129                 response += self.whois(query, nhost, 0)
   123             return response
   130             return response
   124 
   131 
       
   132 
   125     def choose_server(self, domain):
   133     def choose_server(self, domain):
   126         """Choose initial lookup NIC host"""
   134         """Choose initial lookup NIC host"""
   127         if type(domain) is not unicode:
   135         if type(domain) is not str:
   128             domain = domain.decode('utf-8').encode('idna')
   136             domain = domain.decode('utf-8').encode('idna').decode('utf-8')
   129         if domain.endswith("-NORID"):
   137         if domain.endswith("-NORID"):
   130             return NICClient.NORIDHOST
   138             return NICClient.NORIDHOST
   131         pos = domain.rfind('.')
   139         pos = domain.rfind('.')
   132         if pos == -1:
   140         if pos == -1:
   133             return None
   141             return None
   236     flags = 0
   244     flags = 0
   237     nic_client = NICClient()
   245     nic_client = NICClient()
   238     options, args = parse_command_line(sys.argv)
   246     options, args = parse_command_line(sys.argv)
   239     if options.b_quicklookup:
   247     if options.b_quicklookup:
   240         flags = flags | NICClient.WHOIS_QUICK
   248         flags = flags | NICClient.WHOIS_QUICK
   241     print nic_client.whois_lookup(options.__dict__, args[1], flags)
   249     print(nic_client.whois_lookup(options.__dict__, args[1], flags))