diff --git a/whois/parser.py b/whois/parser.py index 4478478..c59a481 100644 --- a/whois/parser.py +++ b/whois/parser.py @@ -11,8 +11,14 @@ import re import sys import os +import logging + class Parser(object): - def __init__(self, domain, text, whoisServer=None): + def __init__(self, domain, text, whoisServer=None, debug=False): + if debug: + logging.basicConfig(level=logging.DEBUG) + logging.debug("__init__: DEBUG is set to True") + self.domain = domain self.text = text self.whoisServer = whoisServer and whoisServer or "default" @@ -22,7 +28,11 @@ class Parser(object): self.currPath = os.path.dirname(os.path.realpath(__file__)) self.tldPath = os.path.join(self.currPath, "tlds") + logging.debug("__init__: Setting initial variables...\nself.domain: %s\nself.text = %s\nself.whoisServer = %s\nself.tld = %s\nself.currPath = %s\nself.tldPath = %s" + %(self.domain, self.text, self.whoisServer, self.tld, self.currPath, self.tldPath)) + self.parseDefaultConf = {} + logging.debug("__init__: Loading default tld configuration file") execfile(os.path.join(self.tldPath, "default"), {}, self.parseDefaultConf) self.parseDefaultConf = self.parseDefaultConf.get("parse") @@ -75,6 +85,7 @@ class Parser(object): self.parseConf = {} if "LoadConf" in _parseConf: + logging.debug("__init__: LoadConf found in parser config") try: # / # e.g. org/whois.publicinternetregistry.net @@ -84,6 +95,9 @@ class Parser(object): lcWS = lc[1] lcConf = {} + + logging.debug("__init__: Loading configuration file of tld name %s"%(lcTLD)) + execfile("tlds/%s"%(lcTLD), {}, lcConf) lcConf = lcConf.get("parse") @@ -97,13 +111,18 @@ class Parser(object): except: self.parseConf = self.parseDefaultConf.get("default") - def run(self, TEST=False): + + logging.debug("__init__: self.parseConf = %s"%(self.parseConf)) + + + def run(self): result = {} for key in self.parseConf: matches = re.findall(self.parseConf[key], self.text, re.MULTILINE) if matches: + logging.debug("run: regex matches found for key %s. %s"%(key, matches)) result.update({key: map(lambda x: x.strip(), matches)}) - if TEST and not matches: print "No match for %s"%(key) + logging.debug("run: No match for %s"%(key)) print result diff --git a/whois/whois.py b/whois/whois.py index 43e46be..35f1917 100644 --- a/whois/whois.py +++ b/whois/whois.py @@ -10,11 +10,16 @@ import sys import os import socket import re +import logging import error class Whois(object): - def __init__(self, domain): + def __init__(self, domain, debug=False): + if debug: + logging.basicConfig(level=logging.DEBUG) + logging.debug("__init__: DEBUG is set to True") + self.domain = domain self.tld = self.domain.split(".")[-1] @@ -22,32 +27,48 @@ class Whois(object): self.tldPath = os.path.join(self.currPath, "tlds") self.tldList = os.listdir(self.tldPath) + logging.debug("__init__: Setting initial variables.. self.currPath = %s / self.tldPath = %s / self.tldList = %s" + %(self.currPath, self.tldPath, self.tldList)) + self.settings = {} if self.tld in self.tldList: + logging.debug("__init__: Loading tld configuration file...") + _settings = {} execfile(os.path.join(self.tldPath, self.tld), {}, _settings) if "server" in _settings: + logging.debug("__init__: Settings: %s"%(_settings["server"])) self.settings.update(_settings["server"]) + else: + logging.debug("__init__: No server settings found") def chooseServer(self): if "host" in self.settings: + logging.debug("chooseServer: Whois server addr: %s"%(self.settings["host"])) return self.settings["host"] else: + logging.debug("chooseServer: Whois server addr: %s"%(self.tld + ".whois-servers.net")) return self.tld + ".whois-servers.net" def sendQuery(self, whoisServer): + logging.debug("sendQuery: Connecting to whois server") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((whoisServer, 43)) + except: # FIXME: Create a exception class for this - print "ERROR Could not connect to whois server %s"%(whoisServer) + logging.error("sendQuery: Error connecting to whois server %s"%(whoisServer)) return False - - s.send(self.domain + "\r\n") + + msg = self.domain + "\r\n" + + logging.debug("sendQuery: Sending data.. %s"%(msg)) + + s.send(msg) result = "" @@ -59,13 +80,19 @@ class Whois(object): result += buffer - return result.replace("\r\n", "\n") + finalResult = result.replace("\r\n", "\n") + + logging.debug("sendQuery: result: %s"%(finalResult)) + + return finalResult def query(self, redirect=True): whoisServer = self.chooseServer() result = self.sendQuery(whoisServer) if redirect and "redirect" in self.settings: + logging.debug("query: Redirection found. Connecting to given server address") + redirection = re.findall(self.settings["redirect"], result, re.MULTILINE) while redirection and len(redirection) >= 1: