Query IP whois info in python

This is a simple python script  based on some former rwhois code. It shows us an easy way on how to get detailed information about a specific IP address:

joseph@e54:~$ python ip-whois.py
====== whois.arin.net                        

OrgName:    RIPE Network Coordination Centre
OrgID:      RIPE
Address:    P.O. Box 10096
City:       Amsterdam
PostalCode: 1001EB
Country:    NL

ReferralServer: whois://whois.ripe.net:43

NetRange: -
NetName:    94-RIPE
NetHandle:  NET-94-0-0-0-1
NetType:    Allocated to RIPE NCC
NameServer: SEC1.APNIC.NET
NameServer: SEC3.APNIC.NET
NameServer: NS2.LACNIC.NET
Comment:    These addresses have been further assigned to users in
Comment:    the RIPE NCC region. Contact information can be found in
Comment:    the RIPE database at http://www.ripe.net/whois
RegDate:    2007-07-30
Updated:    2009-05-18

The source code is listed below. You may copy the code into a text file and name it as ip-query.py:

#!/usr/bin/env python
# Forwarded version from thomasfischer.biz!!!
import os, sys, string, time, getopt, socket, select, re, errno, copy, signal        

def queryWhois(query, server='whois.ripe.net'):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while 1:
            s.connect((server, 43))
        except socket.error, (ecode, reason):
            if ecode==errno.EINPROGRESS:
            elif ecode==errno.EALREADY:
                raise socket.error, (ecode, reason)

    ret = select.select ([s], [s], [], 30)

    if len(ret[1])== 0 and len(ret[0]) == 0:
        raise TimedOut, "on data"           


    s.send("%s\n" % query)
    page = ""
    while 1:
        data = s.recv(8196)
        if not data: break
        page = page + data


    if string.find(page, "IANA-BLK") != -1:
        raise 'no match'

    if string.find(page, "Not allocated by APNIC") != -1:
        raise 'no match'

    return page

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "usage: %s " % sys.argv[0]
    ip = sys.argv[1]

    for server in ['whois.arin.net', 'whois.ripe.net', 'whois.apnic.net', 'whois.lacnic.net', 'whois.afrinic.net']:
            res = queryWhois(ip, server)
            print '======', server
            print res
            break # we only need the info once

If you wanna find whois information about some domain names, you might need to re-write these open-sourced scripts. An suggested alternative choice is pywhois which is hosted by code.google.com.

Update: The whois query can now be done via our online interface as well: http://whois.admon.org/

Share Button

7 thoughts on “Query IP whois info in python

  1. Hello, after reading this remarkable piece of writing i am as well delighted
    to share my knowledge here with friends.

  2. Hi, I log on to your new stuff regularly. Your writing style is awesome, keep up
    the good work!

  3. Hi there,

    Juste wanted to tell you that there is a little mistake when you send the request.
    You forgot the backlash ‘\n’. Therefore the request isn’t well formated.

    Otherwise I really thank you for that piece of code. It’s excatly what I was looking for 🙂

  4. I apenas não poderia ir embora o seu website
    antes sugerindo que eu realmente amei usual informação uma pessoa oferta na
    sua convidados? É vai ser novamente incessantemente , a
    fim de inspecionar novas mensagens

Leave a comment

Your email address will not be published. Required fields are marked *