whois/whois.py
author Richard Baron Penman
Tue, 08 Jan 2013 18:23:18 +1100
changeset 12 c57439b500cb
parent 6 pywhois/whois.py@7dee244ba3ef
child 32 5f851e9c196a
permissions -rw-r--r--
fixed test cases
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     1
"""
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     2
Whois client for python
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     3
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     4
transliteration of:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     5
http://www.opensource.apple.com/source/adv_cmds/adv_cmds-138.1/whois/whois.c
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     6
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     7
Copyright (c) 2010 Chris Wolf
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     8
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
     9
Permission is hereby granted, free of charge, to any person obtaining a copy
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    10
of this software and associated documentation files (the "Software"), to deal
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    11
in the Software without restriction, including without limitation the rights
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    13
copies of the Software, and to permit persons to whom the Software is
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    14
furnished to do so, subject to the following conditions:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    15
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    16
The above copyright notice and this permission notice shall be included in
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    17
all copies or substantial portions of the Software.
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    18
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    25
THE SOFTWARE.
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    26
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    27
  Last edited by:  $Author$
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    28
              on:  $DateTime$
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    29
        Revision:  $Revision$
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    30
              Id:  $Id$
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    31
          Author:  Chris Wolf
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    32
"""
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    33
import sys
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    34
import socket
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    35
import optparse
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    36
#import pdb
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    37
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    38
def enforce_ascii(a):
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    39
    if isinstance(a, str) or isinstance(a, unicode):
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    40
        # return a.encode('ascii', 'replace')
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    41
        r = ""
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    42
        for i in a:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    43
            if ord(i) >= 128:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    44
                r += "?"
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    45
            else:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    46
                r += i
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    47
        return r
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    48
    else:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    49
        return a
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    50
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    51
class NICClient(object) :
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    52
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    53
    ABUSEHOST           = "whois.abuse.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    54
    NICHOST             = "whois.crsnic.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    55
    INICHOST            = "whois.networksolutions.com"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    56
    DNICHOST            = "whois.nic.mil"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    57
    GNICHOST            = "whois.nic.gov"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    58
    ANICHOST            = "whois.arin.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    59
    LNICHOST            = "whois.lacnic.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    60
    RNICHOST            = "whois.ripe.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    61
    PNICHOST            = "whois.apnic.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    62
    MNICHOST            = "whois.ra.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    63
    QNICHOST_TAIL       = ".whois-servers.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    64
    SNICHOST            = "whois.6bone.net"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    65
    BNICHOST            = "whois.registro.br"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    66
    NORIDHOST           = "whois.norid.no"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    67
    IANAHOST            = "whois.iana.org"
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    68
    DENICHOST           = "de.whois-servers.net"
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    69
    DEFAULT_PORT        = "nicname"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    70
    WHOIS_SERVER_ID     = "Whois Server:"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    71
    WHOIS_ORG_SERVER_ID = "Registrant Street1:Whois Server:"
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    72
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    73
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    74
    WHOIS_RECURSE       = 0x01
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    75
    WHOIS_QUICK         = 0x02
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    76
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    77
    ip_whois = [ LNICHOST, RNICHOST, PNICHOST, BNICHOST ]
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    78
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    79
    def __init__(self) :
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    80
        self.use_qnichost = False
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    81
       
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    82
    def findwhois_server(self, buf, hostname):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    83
        """Search the initial TLD lookup results for the regional-specifc
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    84
        whois server for getting contact details.
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    85
        """
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    86
        #print 'finding whois server'
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    87
        #print 'parameters:', buf, 'hostname', hostname
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    88
        nhost = None
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    89
        parts_index = 1
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    90
        start = buf.find(NICClient.WHOIS_SERVER_ID)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    91
        #print 'start', start
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    92
        if (start == -1):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    93
            start = buf.find(NICClient.WHOIS_ORG_SERVER_ID)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    94
            parts_index = 2
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    95
       
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    96
        if (start > -1):  
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    97
            end = buf[start:].find('\n')
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    98
            #print 'end:', end
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    99
            whois_line = buf[start:end+start]
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   100
            #print 'whois_line', whois_line
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   101
            nhost = whois_line.split(NICClient.WHOIS_SERVER_ID+' ').pop()
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   102
            nhost = nhost.split('http://').pop()
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   103
            #if the whois address is domain.tld/something then
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   104
            #s.connect((hostname, 43)) does not work
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   105
            if nhost.count('/') > 0:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   106
                nhost = None
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   107
            #print 'nhost:',nhost
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   108
        elif (hostname == NICClient.ANICHOST):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   109
            for nichost in NICClient.ip_whois:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   110
                if (buf.find(nichost) != -1):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   111
                    nhost = nichost
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   112
                    break
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   113
        return nhost
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   114
       
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   115
    def whois(self, query, hostname, flags):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   116
        """Perform initial lookup with TLD whois server
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   117
        then, if the quick flag is false, search that result
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   118
        for the region-specifc whois server and do a lookup
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   119
        there for contact details
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   120
        """
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   121
        #print 'Performing the whois'
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   122
        #print 'parameters given:', query, hostname, flags
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   123
        #pdb.set_trace()
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   124
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   125
        s.connect((hostname, 43))
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   126
        """send takes bytes as an input
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   127
        """
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   128
        queryBytes = None
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   129
        if (hostname == NICClient.DENICHOST):
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   130
            #print 'the domain is in NIC DENIC'
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   131
            queryBytes = ("-T dn,ace -C UTF-8 " + query + "\r\n").encode()
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   132
            #print 'queryBytes:', queryBytes
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   133
        else:
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   134
            queryBytes = (query  + "\r\n").encode()        
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   135
        s.send(queryBytes)
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   136
        """recv returns bytes
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   137
        """
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   138
        #print s
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   139
        response = b''
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   140
        while True:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   141
            d = s.recv(4096)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   142
            response += d
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   143
            if not d:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   144
                break
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   145
        s.close()
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   146
        #pdb.set_trace()
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   147
        nhost = None
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   148
        #print 'response', response
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   149
        response = enforce_ascii(response)
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   150
        if (flags & NICClient.WHOIS_RECURSE and nhost == None):
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   151
            #print 'Inside first if'
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   152
            nhost = self.findwhois_server(response.decode(), hostname)
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   153
            #print 'nhost is:', nhost
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   154
        if (nhost != None):
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   155
            #print 'inside second if'
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   156
            response += self.whois(query, nhost, 0)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   157
            #print 'response', response
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   158
        #print 'returning whois response'
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   159
        return response.decode()
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   160
   
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   161
    def choose_server(self, domain):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   162
        """Choose initial lookup NIC host"""
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   163
        if (domain.endswith("-NORID")):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   164
            return NICClient.NORIDHOST
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   165
        pos = domain.rfind('.')
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   166
        if (pos == -1):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   167
            return None
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   168
        tld = domain[pos+1:]
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   169
        if (tld[0].isdigit()):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   170
            return NICClient.ANICHOST
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   171
   
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   172
        return tld + NICClient.QNICHOST_TAIL
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   173
   
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   174
    def whois_lookup(self, options, query_arg, flags):
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   175
        """Main entry point: Perform initial lookup on TLD whois server,
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   176
        or other server to get region-specific whois server, then if quick
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   177
        flag is false, perform a second lookup on the region-specific
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   178
        server for contact records"""
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   179
        #print 'whois_lookup'
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   180
        nichost = None
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   181
        #pdb.set_trace()
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   182
        # this would be the case when this function is called by other than main
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   183
        if (options == None):                    
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   184
            options = {}
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   185
     
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   186
        if ( (not 'whoishost' in options or options['whoishost'] == None)
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   187
            and (not 'country' in options or options['country'] == None)):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   188
            self.use_qnichost = True
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   189
            options['whoishost'] = NICClient.NICHOST
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   190
            if ( not (flags & NICClient.WHOIS_QUICK)):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   191
                flags |= NICClient.WHOIS_RECURSE
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   192
           
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   193
        if ('country' in options and options['country'] != None):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   194
            result = self.whois(query_arg, options['country'] + NICClient.QNICHOST_TAIL, flags)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   195
        elif (self.use_qnichost):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   196
            nichost = self.choose_server(query_arg)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   197
            if (nichost != None):
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   198
                result = self.whois(query_arg, nichost, flags)
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   199
        else:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   200
            result = self.whois(query_arg, options['whoishost'], flags)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   201
        #print 'whois_lookup finished'
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   202
        return result
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   203
#---- END OF NICClient class def ---------------------
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   204
   
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   205
def parse_command_line(argv):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   206
    """Options handling mostly follows the UNIX whois(1) man page, except
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   207
    long-form options can also be used.
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   208
    """
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   209
    flags = 0
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   210
   
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   211
    usage = "usage: %prog [options] name"
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   212
           
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   213
    parser = optparse.OptionParser(add_help_option=False, usage=usage)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   214
    parser.add_option("-a", "--arin", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   215
                      const=NICClient.ANICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   216
                      help="Lookup using host " + NICClient.ANICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   217
    parser.add_option("-A", "--apnic", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   218
                      const=NICClient.PNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   219
                      help="Lookup using host " + NICClient.PNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   220
    parser.add_option("-b", "--abuse", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   221
                      const=NICClient.ABUSEHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   222
                      help="Lookup using host " + NICClient.ABUSEHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   223
    parser.add_option("-c", "--country", action="store",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   224
                      type="string", dest="country",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   225
                      help="Lookup using country-specific NIC")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   226
    parser.add_option("-d", "--mil", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   227
                      const=NICClient.DNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   228
                      help="Lookup using host " + NICClient.DNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   229
    parser.add_option("-g", "--gov", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   230
                      const=NICClient.GNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   231
                      help="Lookup using host " + NICClient.GNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   232
    parser.add_option("-h", "--host", action="store",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   233
                      type="string", dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   234
                       help="Lookup using specified whois host")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   235
    parser.add_option("-i", "--nws", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   236
                      const=NICClient.INICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   237
                      help="Lookup using host " + NICClient.INICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   238
    parser.add_option("-I", "--iana", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   239
                      const=NICClient.IANAHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   240
                      help="Lookup using host " + NICClient.IANAHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   241
    parser.add_option("-l", "--lcanic", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   242
                      const=NICClient.LNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   243
                      help="Lookup using host " + NICClient.LNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   244
    parser.add_option("-m", "--ra", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   245
                      const=NICClient.MNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   246
                      help="Lookup using host " + NICClient.MNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   247
    parser.add_option("-p", "--port", action="store",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   248
                      type="int", dest="port",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   249
                      help="Lookup using specified tcp port")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   250
    parser.add_option("-Q", "--quick", action="store_true",
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   251
                     dest="b_quicklookup",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   252
                     help="Perform quick lookup")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   253
    parser.add_option("-r", "--ripe", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   254
                      const=NICClient.RNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   255
                      help="Lookup using host " + NICClient.RNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   256
    parser.add_option("-R", "--ru", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   257
                      const="ru", dest="country",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   258
                      help="Lookup Russian NIC")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   259
    parser.add_option("-6", "--6bone", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   260
                      const=NICClient.SNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   261
                      help="Lookup using host " + NICClient.SNICHOST)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   262
    parser.add_option("-?", "--help", action="help")
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   263
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   264
       
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   265
    return parser.parse_args(argv)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   266
   
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   267
if __name__ == "__main__":
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   268
    flags = 0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   269
    nic_client = NICClient()
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   270
    (options, args) = parse_command_line(sys.argv)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   271
    if (options.b_quicklookup is True):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   272
        flags = flags|NICClient.WHOIS_QUICK
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   273
    print(nic_client.whois_lookup(options.__dict__, args[1], flags))