looking for a python whois interface

Fredrik Lundh effbot at telia.com
Thu Mar 16 14:06:49 EST 2000


matthew snyder <matthew at analog.pobox.com> wrote:
> i'm looking for an interface to whois that i can use to determine
> if a given domain name is registered.  right now, i'm doing:
>
> # `domain' is already checked for malicious characters
> res = os.popen("%s %s" % (PATH_TO_WHOIS, domain)).read()
>
> then re.match()ing res to see if it contains whatever string
> whois returns today that indicates that the domain exists.
>
> i'd like something a bit more elegant.  any pointers?

here's a simple asyncore-based whois client that can process
a whole bunch of requests in parallel.  just subclass WhoisClient
to suit your own needs.

# whois-example-1.py

import asyncore
import socket

class WhoisRequest(asyncore.dispatcher_with_send):
    # simple whois requestor

    def __init__(self, consumer, query, host, port=43):
        asyncore.dispatcher_with_send.__init__(self)

        self.consumer = consumer
        self.query = query

        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    def handle_connect(self):
        self.send(self.query + "\r\n")

    def handle_expt(self):
        self.close() # connection failed, shutdown
        self.consumer.abort()

    def handle_read(self):
        # get data from server
        self.consumer.feed(self.recv(2048))

    def handle_close(self):
        self.close()
        self.consumer.close()

class WhoisConsumer:

    def __init__(self, host):
        self.text = ""
        self.host = host

    def feed(self, text):
        self.text = self.text + text

    def abort(self):
        print self.host, "=>", "failed"

    def close(self):
        print self.host, "=>"
        print self.text

#
# try it out

for host in ("python.org", "pythonware.com", "pythonwarez.com"):
    consumer = WhoisConsumer(host)
    request = WhoisRequest(consumer, host, "whois.internic.net")

# loop returns when all requests have been processed
asyncore.loop()

</F>





More information about the Python-list mailing list