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

前端 未结 9 981
忘掉有多难
忘掉有多难 2020-12-31 18:20

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:

相关标签:
9条回答
  • 2020-12-31 18:49

    Parsing another webpage woulnd't be as bad (assuming their html woulnd't be very bad), but it would actually tie me to them - if they're down, I'm down :)

    Actually I found some old project on sourceforge: rwhois.py. What scares me a bit is that their last update is from 2003. But, it might seem as a good place to start reimplementation of what I do right now... Well, I felt obligued to post the link to this project anyway, just for further reference.

    0 讨论(0)
  • 2020-12-31 18:50

    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. :)

    0 讨论(0)
  • 2020-12-31 18:52

    I don't know if gwhois does something special with the server output; however, you can plainly connect to the whois server on port whois (43), send your query, read all the data in the reply and parse them. To make life a little easier, you could use the telnetlib.Telnet class (even if the whois protocol is much simpler than the telnet protocol) instead of plain sockets.

    The tricky parts:

    • which whois server will you ask? RIPE, ARIN, APNIC, LACNIC, AFRINIC, JPNIC, VERIO etc LACNIC could be a useful fallback, since they tend to reply with useful data to requests outside of their domain.
    • what are the exact options and arguments for each whois server? some offer help, others don't. In general, plain domain names work without any special options.
    0 讨论(0)
提交回复
热议问题