Prima versione grezza di un server http dentro il whois.

Leonardo [2010-05-23 20:45]
Prima versione grezza di un server http dentro il whois.
Filename
pywhois.py
pywhoisd.py
diff --git a/pywhois.py b/pywhois.py
index 619bfb0..cde8303 100644
--- a/pywhois.py
+++ b/pywhois.py
@@ -3,7 +3,9 @@
 #
 # Whois module for the pywhoisd server

-import SocketServer, re, math, syslog
+import SocketServer, re, math, syslog, threading
+from socket import socket, AF_INET, SOCK_STREAM
+from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
 from ipcalc import IP, Network

 def FormatOutput(field, value):
@@ -77,7 +79,7 @@ class WhoisRequestHandler(SocketServer.StreamRequestHandler):
     arrives to the WhoisServer.
     """

-    def handle(self):
+    def handle(self, ):
         # Come prima cosa salutiamo
         hello = self.server.config['welcome_banner']
         if hello:
@@ -264,3 +266,38 @@ class Domain():
     def __init__(self, name, owner, ns, data):
         self.name, self.owner, self.ns = name, owner, ns
         self.data = data
+
+class WhoisHTTPRequestHandler(BaseHTTPRequestHandler):
+
+    def do_GET(self):
+        print self.path[1:]
+        request = self.path[1:].strip()
+        s = socket(AF_INET, SOCK_STREAM)
+        s.connect(("127.0.0.1", 43))
+        s.send (request + "\n")
+        response = s.recv(1024)
+        s.close ()
+        self.send_response(200)
+        self.send_header('Content-type', 'text/html')
+        self.end_headers()
+        self.wfile.write(re.sub("\n", "<br>\n", response))
+        return
+
+class BackgroundWhoisHTTPServer(threading.Thread):
+
+    def __init__(self, whoisserver):
+        threading.Thread.__init__(self)
+
+    def run(self):
+        self.server = HTTPServer(("0.0.0.0", 80), WhoisHTTPRequestHandler)
+        self.server.serve_forever()
+
+    def shutdown(self):
+        self.server.socket.close ()
+        self.server.shutdown()
+
+
+
+
+
+
diff --git a/pywhoisd.py b/pywhoisd.py
index a91ae37..b82a656 100755
--- a/pywhoisd.py
+++ b/pywhoisd.py
@@ -31,9 +31,16 @@ if __name__ == "__main__":
     # Prepare the real server, listening to the whole world
     host, port = options.host, int(options.port)
     server = pywhois.WhoisServer((host, port), logger, config)
+    http_server = pywhois.BackgroundWhoisHTTPServer((host, port))
     try:
+        print "Starting http server...",
+        http_server.start()
+        print "done"
+        print "Starting whois server...",
         server.serve_forever()
     except KeyboardInterrupt:
+        http_server.shutdown()
+        server.shutdown()
         logger.Log ("pywhoisd daemon exiting now")

ViewGit