Welcome, guest | Sign In | My Account | Store | Cart

Simple whois client

Python, 20 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
"""whois.py

simple whois client
"""

import sys
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("whois.arin.net", 43))
s.send(sys.argv[1] + "\r\n")
response = ''
while True:
    d = s.recv(4096)
    response += d
    if d == '':
        break
s.close()
print
print response

2 comments

Chris Wolf 11 years, 3 months ago  # | flag

Here's a more complete example that functions exactly like the UNIX whois(1) command:

http://code.activestate.com/recipes/577364-whois-client

Vatay Világi Norbert 5 years, 11 months ago  # | flag

Here is with some changes:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket

def whois(domain):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tld = domain.split('.')[-1]
    try:
        s.connect(((tld + '.whois-servers.net'), 43))
    except socket.error as msg:
        return "You entered not valid tld: .%s" % tld

    s.send(domain + '\r\n')
    r = ''
    while True:
        d = s.recv(4096)
        r += d
        if d == '':
            break
    s.close()
    return r.decode('iso-8859-2').encode('utf-8')
Created by Ryan Ozmun on Mon, 26 Jan 2009 (MIT)
Python recipes (4591)
Ryan Ozmun's recipes (1)

Required Modules

Other Information and Tasks