diff --git a/pywhois.py b/pywhois.py
index cde8303..27dcffe 100644
--- a/pywhois.py
+++ b/pywhois.py
@@ -229,7 +229,7 @@ class WhoisServer(SocketServer.ThreadingTCPServer):
for k, v in net.data.items():
output += FormatOutput (k.capitalize(),v)
if len(nets) == 0:
- output = FormatMessage("Address %s not found\n" % ip)
+ output = FormatMessage("Address %s not found" % ip)
return output
def GetParentNetworks(self, ip):
@@ -269,27 +269,81 @@ class Domain():
class WhoisHTTPRequestHandler(BaseHTTPRequestHandler):
+ style_block = """<style>\
+ h1 {
+ color: #993333;
+ font-family: Trebuchet MS;
+ font-size: 30px;
+ border-bottom: 1px solid #993333;
+ }
+
+ p {
+ font-family: Trebuchet MS;
+ font-size: 14px;
+ margin-left: 50px;
+ }
+ </style>
+ """
+
+ def SendHeaders(self):
+ """
+ Send headers
+ """
+ self.send_header('Content-type', 'text/html')
+ self.end_headers()
+
+ def Format(self, string, request):
+ """
+ Format the raw output of the whois server
+ """
+ html = "<html>\n"
+ html += "<head>\n<title>Whois answer for %s</title>\n" % request
+ html += "</head>\n"
+ html += self.style_block
+ html += "<body>\n"
+ html += "<h1>Whois answer for %s</h1>\n<p>" % request
+ html += re.sub(r"\n", "<br>\n", string)
+ html += "</p></body>\n"
+ html += "</html>"
+ return html
+
def do_GET(self):
- print self.path[1:]
+ """
+ Handler GET request
+ """
+ # Estraiamo la richiesta.
request = self.path[1:].strip()
+
+ # Ci connettiamo al demone locale. Questo è
+ # decisamente subottimale, ma per ora non ho
+ # voglia di fare di meglio
s = socket(AF_INET, SOCK_STREAM)
- s.connect(("127.0.0.1", 43))
+ s.connect(self.server.whoisserver)
s.send (request + "\n")
response = s.recv(1024)
s.close ()
+
+ # Ok, finalmente ci siamo
self.send_response(200)
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- self.wfile.write(re.sub("\n", "<br>\n", response))
+
+ # Spediamo headers e poi l'output formattato
+ self.SendHeaders()
+
+ # Output
+ self.wfile.write(self.Format(response, request))
return
class BackgroundWhoisHTTPServer(threading.Thread):
- def __init__(self, whoisserver):
+ def __init__(self, whoisserver, port):
threading.Thread.__init__(self)
+ self.port = port
+ self.whoisserver = whoisserver
def run(self):
- self.server = HTTPServer(("0.0.0.0", 80), WhoisHTTPRequestHandler)
+ self.server = HTTPServer(("0.0.0.0", self.port),
+ WhoisHTTPRequestHandler)
+ self.server.whoisserver = self.whoisserver
self.server.serve_forever()
def shutdown(self):
diff --git a/pywhoisd.py b/pywhoisd.py
index b82a656..e1f44dd 100755
--- a/pywhoisd.py
+++ b/pywhoisd.py
@@ -8,6 +8,13 @@
import pywhois
from optparse import OptionParser
+def SetOption(config, option, field):
+ if option is not None:
+ return option
+ if config.has_key(field):
+ return config[field]
+ return None
+
if __name__ == "__main__":
parser = OptionParser()
@@ -20,6 +27,8 @@ if __name__ == "__main__":
help = "The listening port of the daemon", default = "43")
parser.add_option("-l", "--listen", help="The listening address of the daemon",
default = "0.0.0.0", dest="host")
+ parser.add_option("-t", "--http-port", help="The listening port of the HTTP server",
+ default = 80, dest = "http_port")
(options, args) = parser.parse_args()
@@ -29,9 +38,10 @@ if __name__ == "__main__":
logger = pywhois.Logger(options.verbose)
# Prepare the real server, listening to the whole world
- host, port = options.host, int(options.port)
+ host, port = SetOption(config, options.host, "host"), int(SetOption(config, options.port, "port"))
+ http_port = int(SetOption(config, options.http_port, "http_port"))
server = pywhois.WhoisServer((host, port), logger, config)
- http_server = pywhois.BackgroundWhoisHTTPServer((host, port))
+ http_server = pywhois.BackgroundWhoisHTTPServer((host, port), http_port)
try:
print "Starting http server...",
http_server.start()
@@ -40,6 +50,7 @@ if __name__ == "__main__":
server.serve_forever()
except KeyboardInterrupt:
http_server.shutdown()
+ http_server.join()
server.shutdown()
logger.Log ("pywhoisd daemon exiting now")