Code

HTTP-Server can now daemonize
[nagixsc.git] / nagixsc_conf2http.py
index 640539fc98013311286c65ead7f022575501e282..3dece813d29ed6c3ffdf4060f333edbaeb27e365 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/python
 
-import BaseHTTPServer
 import ConfigParser
 import base64
 import optparse
@@ -23,6 +22,8 @@ from nagixsc import *
 parser = optparse.OptionParser()
 
 parser.add_option('-c', '', dest='cfgfile', help='Config file')
+parser.add_option('-d', '--daemon', action='store_true', dest='daemon', help='Daemonize, go to background')
+parser.add_option('', '--nossl', action='store_true', dest='nossl', help='Disable SSL (overwrites config file)')
 
 parser.set_defaults(cfgfile='conf2http.cfg')
 
@@ -40,9 +41,10 @@ config = {}
 try:
        config['ip']   = cfgread.get('server', 'ip')
        config['port'] = cfgread.getint('server', 'port')
+       config['ssl']  = cfgread.getboolean('server', 'ssl')
+       config['cert'] = cfgread.get('server', 'sslcert')
 
        config['conf_dir']         = cfgread.get('server', 'conf_dir')
-       config['conf2xml_cmdline'] = cfgread.get('server', 'conf2xml_cmdline')
 
 except ConfigParser.NoOptionError, e:
        print 'Config file error: %s ' % e
@@ -54,7 +56,7 @@ for u in cfgread.options('users'):
 
 ##############################################################################
 
-class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+class Conf2HTTPHandler(MyHTTPRequestHandler):
 
        def http_error(self, code, output):
                self.send_response(code)
@@ -65,8 +67,6 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
 
        def do_GET(self):
-               cmdline = config['conf2xml_cmdline']
-
                path = self.path.split('/')
 
                # Check Basic Auth
@@ -126,8 +126,18 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
 
 def main():
+       if options.nossl:
+               config['ssl'] = False
+
+       if config['ssl'] and not os.path.isfile(config['cert']):
+               print 'SSL certificate "%s" not found!' % config['cert']
+               sys.exit(127)
+
+       if options.daemon:
+               daemonize(pidfile='/var/run/nagixsc_conf2http.pid')
+
+       server = MyHTTPServer((config['ip'], config['port']), Conf2HTTPHandler, ssl=config['ssl'], sslpemfile=config['cert'])
        try:
-               server = BaseHTTPServer.HTTPServer((config['ip'], config['port']), Conf2HTTPHandler)
                server.serve_forever()
        except:
                server.socket.close()