Aggiunto supporto per un messaggio nel file di configurazione.

Leonardo [2010-05-24 16:49]
Aggiunto supporto per un messaggio nel file di configurazione.
Filename
pywhois.py
pywhoisd.conf
pywhoisd.py
diff --git a/pywhois.py b/pywhois.py
index ba08465..fe93cc1 100644
--- a/pywhois.py
+++ b/pywhois.py
@@ -341,7 +341,12 @@ class WhoisHTTPRequestHandler(BaseHTTPRequestHandler):
         try:
             request = re.findall(r"/?query=([^\s]+)", self.path)[0]
         except:
-            response = "Please submit a query"
+            if self.server.mydata['initial_message'] is not None:
+                response = FormatMessage(self.server.mydata['initial_message'])
+            else:
+                response =  ":: Please submit a query.\n"
+                response += ":: You can ask information about a domain, like mydomain.hey.gnet,\n"
+                response += ":: an IP address, like 6.26.23.34 or a network like 6.45.0.0/16.\n"
             request = None

         # Ci connettiamo al demone locale. Questo è
@@ -349,7 +354,7 @@ class WhoisHTTPRequestHandler(BaseHTTPRequestHandler):
         # voglia di fare di meglio
         if request:
             s = socket(AF_INET, SOCK_STREAM)
-            s.connect(self.server.whoisserver)
+            s.connect(self.server.mydata['whoisserver'])
             s.send (request + "\n")
             response = s.recv(1024)
             s.close ()
@@ -366,15 +371,20 @@ class WhoisHTTPRequestHandler(BaseHTTPRequestHandler):

 class BackgroundWhoisHTTPServer(threading.Thread):

-    def __init__(self, whoisserver, port):
+    def __init__(self, whoisserver, port, initial_message):
         threading.Thread.__init__(self)
         self.port = port
         self.whoisserver = whoisserver
-
+        self.initial_message = initial_message
     def run(self):
         self.server = HTTPServer(("0.0.0.0", self.port),
                                  WhoisHTTPRequestHandler)
-        self.server.whoisserver = self.whoisserver
+
+        # Inseriamo i dati interessanti all'interno del webserver
+        # in modo che l'handler possa recuperarli per generare
+        # le eventuali risposte
+        self.server.mydata = { 'whoisserver': self.whoisserver,
+                               'initial_message': self.initial_message }
         self.server.serve_forever()

     def shutdown(self):
diff --git a/pywhoisd.conf b/pywhoisd.conf
index 12b5f07..a96d444 100644
--- a/pywhoisd.conf
+++ b/pywhoisd.conf
@@ -16,3 +16,16 @@ welcome_banner = This is the whoisd server for the GNet
  of our network visit us  at http://linus.robol.it/gnet/
  or (if you have access now)  http://linus.leo.gnet/gnet/.
  See you soon!;
+
+# Initial message for the web interface
+initial_message = Please submit a query;
+
+# Whois port
+port = 8043;
+
+# Web interface port
+http_port = 8080;
+
+
+
+
diff --git a/pywhoisd.py b/pywhoisd.py
index e1f44dd..4682913 100755
--- a/pywhoisd.py
+++ b/pywhoisd.py
@@ -24,11 +24,11 @@ if __name__ == "__main__":
                       default="/etc/pywhoisd.conf",
                       help = "Set config file", metavar="CONFIG_FILE")
     parser.add_option("-p", "--port", dest="port",
-                      help = "The listening port of the daemon", default = "43")
+                      help = "The listening port of the daemon", default = None)
     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")
+                      default = None, dest = "http_port")

     (options, args) = parser.parse_args()

@@ -41,12 +41,13 @@ if __name__ == "__main__":
     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_port)
+
+    # Messaggio di benvenuto per chi accede all'interfaccia
+    # web.
+    initial_message = config['initial_message']
+    http_server = pywhois.BackgroundWhoisHTTPServer((host, port), http_port, initial_message)
     try:
-        print "Starting http server...",
         http_server.start()
-        print "done"
-        print "Starting whois server...",
         server.serve_forever()
     except KeyboardInterrupt:
         http_server.shutdown()
ViewGit