Jan 09
A little Whois reader I wrote while messing around with sockets in Python. It doesn’t do any processing on the reply, it just returns the whole thing as a string. It’s also missing any mechanism to determine which Whois servers to use for which TLDs. But anyway, it’s a good start!
#!/usr/bin/env python import socket import select PORT = 43 BUFSIZE = 1024 LINEEND = '\r\n' def whois(domain, server, port=PORT): ''' Perform a WHOIS search for domain on server/port. ''' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((server, port)) msg = '' lookup = domain + LINEEND readable, writable, error = select.select([],[s],[],60) if s in writable: s.send(lookup) readable, writable, error = select.select([s],[],[],60) while s in readable: data = s.recv(1024) msg = msg + data if not data: s.shutdown(socket.SHUT_RDWR) s.close() break readable, writable, error = select.select([s],[],[],60) else: s.shutdown(socket.SHUT_RDRW) s.close() return msg.strip() if __name__ == '__main__': print whois('google.com', 'whois.markmonitor.com')
