Query domain name whois info by Python

In one of our former post, we give an example on how to query IP whois info by Python, here is an example on how to query whois information for a domain name.

#!/usr/bin/python
#encoding = utf8
#Author:hysia

import sys
import socket

PORT  =  43
WhoisServers = {'com':'whois.internic.net',
              'net':'whois.internic.net',
              'org':'whois.pir.org',
              'nfo':'whois.afilias.info',
              'biz':'whois.biz',
              '.cc':'whois.nic.cc',
              'edu':'rs.internic.net',
              'mil':'whois.nic.mil',
              'gov':'whois.nic.gov',
              '.uk':'whois.nic.uk',
              '.us':'whois.nic.us',
              'ame':'whois.nic.name',
              'eum':'whois.museum',
              '.su':'whois.ripn.net',
              '.ru':'whois.nic.ru',
              'int':'whois.iana.org',
              '.ws':'whois.worldsite.ws',
              '.kr':'whois.krnic.net',
              '.jp':'whois.nic.ad.jp',
              '.it':'whois.nic.it',
              '.de':'whois.denic.de',
              '.fr':'whois.nic.fr',
              '.ca':'whois.cira.ca',
              '.cn':'whois.cnnic.net.cn',
              '.tw':'whois.twnic.net.tw',
              '.hk':'whois.hkdnr.net.hk',
              '.au':'whois.aunic.net',
              '.ac':'whois.nic.ac',
              'DEF':'whois.verisign-grs.com'}
              #'DEF':'whois.apin.net'}

fulldomain = sys.argv[1]

if fulldomain.startswith('www.'):
    fulldomain = fulldomain[4:]

domain = fulldomain[-3:]
print 'Domain: ',fulldomain  
if not WhoisServers.get(domain):
    whoisserver = WhoisServers['DEF']
else:
    whoisserver = WhoisServers[domain]
print whoisserver

try:            
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((whoisserver,PORT))
    s.send(fulldomain + "\r\n")
    response = ''

    while True:
        data = s.recv(4096)
        response += data
        if data == '':
            break
    s.close()  
    infomation = response.lower()
    try:
        pos = infomation.find('expiration date: ')
        expirDate = infomation[pos+17:pos+28]
        print 'Expiration Date:',expirDate
    except:
        print 'unkown'

except:
    print 'time out'

Looks smart! But never mind that the whois info cannot be returned correctly sometimes. We also have a online whois query script here, which is created by PHP.

The script is copied from here: http://blog.hysia.com/

4 thoughts on “Query domain name whois info by Python

  1. I’d have to examine with you here. Which isn’t something I often do! I get pleasure from studying a put up that may make folks think. Also, thanks for permitting me to comment!

  2. Everything is very open with a very clear clarification of the
    issues. It was really informative. Your website is
    useful. Thank you for sharing!

  3. I have noticed you don’t monetize your site, don’t waste your traffic,
    you can earn extra bucks every month because you’ve got high quality content.
    If you want to know how to make extra money, search for: Boorfe’s tips best adsense alternative

Leave a comment

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