whois/whois.py
author Richard Penman
Mon, 14 Sep 2015 15:13:50 +0100
changeset 64 2ed54e885571
parent 62 fc06c601b875
child 65 50b5966b5566
permissions -rw-r--r--
only focus whois search after find many results, to avoid search failure
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
"""
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    27
import re
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    28
import sys
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    29
import socket
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    30
import optparse
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    31
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    32
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    33
def enforce_ascii(a):
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    34
    if isinstance(a, str) or isinstance(a, unicode):
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    35
        r = ""
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    36
        for i in a:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    37
            if ord(i) >= 128:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    38
                r += "?"
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    39
            else:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    40
                r += i
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    41
        return r
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    42
    else:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    43
        return a
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    44
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    45
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    46
class NICClient(object):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    47
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    48
    ABUSEHOST = "whois.abuse.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    49
    NICHOST = "whois.crsnic.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    50
    INICHOST = "whois.networksolutions.com"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    51
    DNICHOST = "whois.nic.mil"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    52
    GNICHOST = "whois.nic.gov"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    53
    ANICHOST = "whois.arin.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    54
    LNICHOST = "whois.lacnic.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    55
    RNICHOST = "whois.ripe.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    56
    PNICHOST = "whois.apnic.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    57
    MNICHOST = "whois.ra.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    58
    QNICHOST_TAIL = ".whois-servers.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    59
    SNICHOST = "whois.6bone.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    60
    BNICHOST = "whois.registro.br"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    61
    NORIDHOST = "whois.norid.no"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    62
    IANAHOST = "whois.iana.org"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    63
    DENICHOST = "de.whois-servers.net"
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    64
    DEFAULT_PORT = "nicname"
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    65
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    66
    WHOIS_RECURSE = 0x01
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    67
    WHOIS_QUICK = 0x02
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    68
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    69
    ip_whois = [LNICHOST, RNICHOST, PNICHOST, BNICHOST]
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    70
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    71
    def __init__(self):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    72
        self.use_qnichost = False
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    73
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    74
    def findwhois_server(self, buf, hostname, query):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    75
        """Search the initial TLD lookup results for the regional-specifc
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    76
        whois server for getting contact details.
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    77
        """
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    78
        nhost = None
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    79
        match = re.compile('Domain Name: ' + query + '\s*.*?Whois Server: (.*?)\s', flags=re.IGNORECASE|re.DOTALL).search(buf)
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    80
        if match:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    81
            nhost = match.groups()[0]
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    82
            # if the whois address is domain.tld/something then
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    83
            # s.connect((hostname, 43)) does not work
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    84
            if nhost.count('/') > 0:
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    85
                nhost = None
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    86
        elif hostname == NICClient.ANICHOST:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    87
            for nichost in NICClient.ip_whois:
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
    88
                if buf.find(nichost) != -1:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    89
                    nhost = nichost
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    90
                    break
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    91
        return nhost
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
    92
64
2ed54e885571 only focus whois search after find many results, to avoid search failure
Richard Penman
parents: 62
diff changeset
    93
    def whois(self, query, hostname, flags, many_results=False):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    94
        """Perform initial lookup with TLD whois server
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
    95
        then, if the quick flag is false, search that result
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    96
        for the region-specifc whois server and do a lookup
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    97
        there for contact details
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
    98
        """
52
1f80f6dec7ac add socket timeout exception
Jasey Wang <jaseywang@gmail.com>
parents: 45
diff changeset
    99
        try:
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   100
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   101
            s.settimeout(10)
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   102
            s.connect((hostname, 43))
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   103
            # end takes bytes as an input
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   104
            queryBytes = None
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   105
            if type(query) is not unicode:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   106
                query = query.decode('utf-8')
34
f9da616f15cf Pass all domains through encode('idna') in NICClient
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 32
diff changeset
   107
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   108
            if hostname == NICClient.DENICHOST:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   109
                queryBytes = "-T dn,ace -C UTF-8 " + query
64
2ed54e885571 only focus whois search after find many results, to avoid search failure
Richard Penman
parents: 62
diff changeset
   110
            elif hostname.endswith(NICClient.QNICHOST_TAIL) and many_results:
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   111
                queryBytes = '=' + query
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   112
            else:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   113
                queryBytes = query
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   114
            s.send((queryBytes + "\r\n").encode('idna'))
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   115
            # recv returns bytes
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   116
            response = b''
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   117
            while True:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   118
                d = s.recv(4096)
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   119
                response += d
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   120
                if not d:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   121
                    break
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   122
            s.close()
52
1f80f6dec7ac add socket timeout exception
Jasey Wang <jaseywang@gmail.com>
parents: 45
diff changeset
   123
        except socket.error as socketerror:
64
2ed54e885571 only focus whois search after find many results, to avoid search failure
Richard Penman
parents: 62
diff changeset
   124
            print 'Socket Error:', socketerror
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   125
        nhost = None
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   126
        response = enforce_ascii(response)
64
2ed54e885571 only focus whois search after find many results, to avoid search failure
Richard Penman
parents: 62
diff changeset
   127
        if 'with "=xxx"' in response:
2ed54e885571 only focus whois search after find many results, to avoid search failure
Richard Penman
parents: 62
diff changeset
   128
            return self.whois(query, hostname, flags, True)
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   129
        if flags & NICClient.WHOIS_RECURSE and nhost is None:
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   130
            nhost = self.findwhois_server(response.decode(), hostname, query)
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   131
        if nhost is not None:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   132
            response += self.whois(query, nhost, 0)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   133
        return response.decode()
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   134
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   135
    def choose_server(self, domain):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   136
        """Choose initial lookup NIC host"""
36
af839b9c0ed1 Add support for punycode TLDs
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 34
diff changeset
   137
        if type(domain) is not unicode:
af839b9c0ed1 Add support for punycode TLDs
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 34
diff changeset
   138
            domain = domain.decode('utf-8').encode('idna')
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   139
        if domain.endswith("-NORID"):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   140
            return NICClient.NORIDHOST
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   141
        pos = domain.rfind('.')
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   142
        if pos == -1:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   143
            return None
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   144
        tld = domain[pos+1:]
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   145
        if tld[0].isdigit():
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   146
            return NICClient.ANICHOST
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   147
        return tld + NICClient.QNICHOST_TAIL
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   148
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   149
    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
   150
        """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
   151
        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
   152
        flag is false, perform a second lookup on the region-specific
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   153
        server for contact records"""
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   154
        nichost = None
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   155
        # whoud happen when this function is called by other than main
60
7801a420f679 added support for native client
Richard Penman
parents: 52
diff changeset
   156
        if options is None:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   157
            options = {}
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   158
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   159
        if ('whoishost' not in options or options['whoishost'] is None) \
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   160
                and ('country' not in options or options['country'] is None):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   161
            self.use_qnichost = True
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   162
            options['whoishost'] = NICClient.NICHOST
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   163
            if not (flags & NICClient.WHOIS_QUICK):
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   164
                flags |= NICClient.WHOIS_RECURSE
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   165
60
7801a420f679 added support for native client
Richard Penman
parents: 52
diff changeset
   166
        if 'country' in options and options['country'] is not None:
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   167
            result = self.whois(
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   168
                query_arg,
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   169
                options['country'] + NICClient.QNICHOST_TAIL,
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   170
                flags
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   171
            )
60
7801a420f679 added support for native client
Richard Penman
parents: 52
diff changeset
   172
        elif self.use_qnichost:
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   173
            nichost = self.choose_server(query_arg)
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   174
            if nichost is not None:
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   175
                result = self.whois(query_arg, nichost, flags)
45
52ce01013731 Handle experimental lookup method for bad domains
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 36
diff changeset
   176
            else:
52ce01013731 Handle experimental lookup method for bad domains
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 36
diff changeset
   177
                result = ''
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   178
        else:
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   179
            result = self.whois(query_arg, options['whoishost'], flags)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   180
        return result
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   181
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   182
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   183
def parse_command_line(argv):
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   184
    """Options handling mostly follows the UNIX whois(1) man page, except
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   185
    long-form options can also be used.
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   186
    """
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   187
    flags = 0
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   188
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   189
    usage = "usage: %prog [options] name"
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   190
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   191
    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
   192
    parser.add_option("-a", "--arin", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   193
                      const=NICClient.ANICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   194
                      help="Lookup using host " + NICClient.ANICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   195
    parser.add_option("-A", "--apnic", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   196
                      const=NICClient.PNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   197
                      help="Lookup using host " + NICClient.PNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   198
    parser.add_option("-b", "--abuse", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   199
                      const=NICClient.ABUSEHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   200
                      help="Lookup using host " + NICClient.ABUSEHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   201
    parser.add_option("-c", "--country", action="store",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   202
                      type="string", dest="country",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   203
                      help="Lookup using country-specific NIC")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   204
    parser.add_option("-d", "--mil", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   205
                      const=NICClient.DNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   206
                      help="Lookup using host " + NICClient.DNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   207
    parser.add_option("-g", "--gov", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   208
                      const=NICClient.GNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   209
                      help="Lookup using host " + NICClient.GNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   210
    parser.add_option("-h", "--host", action="store",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   211
                      type="string", dest="whoishost",
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   212
                      help="Lookup using specified whois host")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   213
    parser.add_option("-i", "--nws", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   214
                      const=NICClient.INICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   215
                      help="Lookup using host " + NICClient.INICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   216
    parser.add_option("-I", "--iana", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   217
                      const=NICClient.IANAHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   218
                      help="Lookup using host " + NICClient.IANAHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   219
    parser.add_option("-l", "--lcanic", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   220
                      const=NICClient.LNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   221
                      help="Lookup using host " + NICClient.LNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   222
    parser.add_option("-m", "--ra", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   223
                      const=NICClient.MNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   224
                      help="Lookup using host " + NICClient.MNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   225
    parser.add_option("-p", "--port", action="store",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   226
                      type="int", dest="port",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   227
                      help="Lookup using specified tcp port")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   228
    parser.add_option("-Q", "--quick", action="store_true",
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   229
                      dest="b_quicklookup",
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   230
                      help="Perform quick lookup")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   231
    parser.add_option("-r", "--ripe", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   232
                      const=NICClient.RNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   233
                      help="Lookup using host " + NICClient.RNICHOST)
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   234
    parser.add_option("-R", "--ru", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   235
                      const="ru", dest="country",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   236
                      help="Lookup Russian NIC")
6
7dee244ba3ef fixed builtin whois parser for pwhois.rotld.ro
Richard Baron Penman
parents: 0
diff changeset
   237
    parser.add_option("-6", "--6bone", action="store_const",
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   238
                      const=NICClient.SNICHOST, dest="whoishost",
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   239
                      help="Lookup using host " + NICClient.SNICHOST)
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   240
    parser.add_option("-?", "--help", action="help")
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   241
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   242
    return parser.parse_args(argv)
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   243
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   244
0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   245
if __name__ == "__main__":
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   246
    flags = 0
ea0e45971cea initial commit to mercurial
Richard Baron Penman
parents:
diff changeset
   247
    nic_client = NICClient()
62
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   248
    options, args = parse_command_line(sys.argv)
fc06c601b875 whois client was using the first whois server in list rather than the whois server associated with the domain we are querying issue #74
Richard Penman
parents: 60
diff changeset
   249
    if options.b_quicklookup:
32
5f851e9c196a Stylistic fixes on the NICClient class
Evgeni Kunev <evgeni.kunev@gmail.com>
parents: 12
diff changeset
   250
        flags = flags | NICClient.WHOIS_QUICK
60
7801a420f679 added support for native client
Richard Penman
parents: 52
diff changeset
   251
    print nic_client.whois_lookup(options.__dict__, args[1], flags)