pythonsystem-administrationwhois

What Python way would you suggest to check whois database records?


I'm trying to get a webservice up and running that actually requires to check whois databases. What I'm doing right now is ugly and I'd like to avoid it as much as I can: I call gwhois command and parse its output. Ugly.

I did some search to try to find a pythonic way to do this task. Generally I got quite much nothing - this old discussion list link has a way to check if domain exist. Quite not what I was looking for... But still, it was best anwser Google gave me - everything else is just a bunch of unanwsered questions.

Any of you have succeeded to get some method up and running? I'd very much appreciate some tips, or should I just do it the opensource-way, sit down and code something by myself? :)


Solution

  • There's nothing wrong with using a command line utility to do what you want. If you put a nice wrapper around the service, you can implement the internals however you want! For example:

    class Whois(object):
        _whois_by_query_cache = {}
    
        def __init__(self, query):
            """Initializes the instance variables to defaults. See :meth:`lookup`
            for details on how to submit the query."""
            self.query = query
            self.domain = None
            # ... other fields.
    
        def lookup(self):
            """Submits the `whois` query and stores results internally."""
            # ... implementation
    

    Now, whether or not you roll your own using urllib, wrap around a command line utility (like you're doing), or import a third party library and use that (like you're saying), this interface stays the same.

    This approach is generally not considered ugly at all -- sometimes command utilities do what you want and you should be able to leverage them. If speed ends up being a bottleneck, your abstraction makes the process of switching to a native Python implementation transparent to your client code.

    Practicality beats purity -- that's what's Pythonic. :)