faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 8 people (63%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

whois client

Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 380, Chunk Light Fluffy


"""
Packages: networking.internet
"""
"""
> Is there any module that implement all of part of the whois tool ?
Whois clients are very simple minded.  All the real work is done by the
server, so all you really have to do is open the socket, send a one
line query, then read the response until the server closes the socket.
If you want something smarter, or even just a decent list of all the
whois servers, try <URL:http://www.geektools.com/software.html>
"""
import socket, string
WHOIS_PORT = 43
CRLF = "\015\012"
def whois(host, query):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(host, WHOIS_PORT)
    f = s.makefile('rb')
    s.send(query + CRLF)
    rv = []
    while 1:
        line = f.readline()
        if not line:
            break
        if line[-2:] == CRLF:
            line = line[:-2]
        elif line[-1:] in CRLF:
            line = line[:-1]
        rv.append(line)
    f.close()
    s.close()
    return string.join(rv, "\n") + "\n"
print whois("whois.networksolutions.com", "internic.net")