diff options
| author | 2010-04-02 15:02:11 +0200 | |
|---|---|---|
| committer | 2010-04-02 15:02:11 +0200 | |
| commit | feead10fd643cf2c4ab9faff24fc9690e87c1d84 (patch) | |
| tree | ac06d4211f3da23bcbdd6e16c6dc2ca5b7d0b73e /module/web | |
| parent | Litte remove Linklist bugfix (diff) | |
| parent | django embedded (diff) | |
| download | pyload-feead10fd643cf2c4ab9faff24fc9690e87c1d84.tar.xz | |
merged
Diffstat (limited to 'module/web')
| -rw-r--r-- | module/web/ServerThread.py | 40 | ||||
| -rw-r--r-- | module/web/run_fcgi.py | 172 | ||||
| -rwxr-xr-x | module/web/run_server.py | 27 | 
3 files changed, 212 insertions, 27 deletions
| diff --git a/module/web/ServerThread.py b/module/web/ServerThread.py index 718dd6136..aa1f67244 100644 --- a/module/web/ServerThread.py +++ b/module/web/ServerThread.py @@ -8,6 +8,7 @@ from subprocess import call  from sys import version_info  from cStringIO import StringIO  import threading +import sys  class WebServer(threading.Thread):      def __init__(self, pycore): @@ -17,8 +18,9 @@ class WebServer(threading.Thread):          self.server = pycore.config['webinterface']['server']          self.https = pycore.config['webinterface']['https']          self.setDaemon(True) -     +               def run(self): +        sys.path.append(join(self.pycore.path, "module", "web"))          avail = ["builtin"]          host = self.pycore.config['webinterface']['host']          port = self.pycore.config['webinterface']['port'] @@ -90,7 +92,7 @@ class WebServer(threading.Thread):              content = content.replace("%(path)", join(path, "servers"))              content = content.replace("%(host)", host) -            content = content.replace("%(port)", port) +            content = content.replace("%(port)", str(port))              content = content.replace("%(media)", join(path, "media"))              content = content.replace("%(version)", ".".join(map(str, version_info[0:2]))) @@ -107,11 +109,11 @@ class WebServer(threading.Thread):              new_config.write(content)              new_config.close() -            command = ['python', join(self.pycore.path, "module", "web", "manage.py"), "runfcgi", "daemonize=false", "method=threaded", "host=127.0.0.1", "port=9295"] -            self.p = Popen(command, stderr=Output(out), stdin=PIPE, stdout=Output(out)) +            command = ['nginx', '-c', join(path, "servers", "nginx.conf"),] +            self.p = Popen(command, stderr=PIPE, stdin=PIPE, stdout=Output(out)) -            command2 = ['nginx', '-c', join(path, "servers", "nginx.conf"),] -            self.p2 = Popen(command2, stderr=PIPE, stdin=PIPE, stdout=Output(out)) +            import run_fcgi +            run_fcgi.handle("daemonize=false", "method=threaded", "host=127.0.0.1", "port=9295")          elif self.server == "lighttpd": @@ -123,7 +125,7 @@ class WebServer(threading.Thread):              content = content.replace("%(path)", join(path, "servers"))              content = content.replace("%(host)", host) -            content = content.replace("%(port)", port) +            content = content.replace("%(port)", str(port))              content = content.replace("%(media)", join(path, "media"))              content = content.replace("%(version)", ".".join(map(str, version_info[0:2]))) @@ -139,33 +141,35 @@ class WebServer(threading.Thread):              new_config.write(content)              new_config.close() -            command = ['python', join(self.pycore.path, "module", "web", "manage.py"), "runfcgi", "daemonize=false", "method=threaded", "host=127.0.0.1", "port=9295"] -            self.p = Popen(command, stderr=Output(out), stdin=PIPE, stdout=Output(out)) +            command = ['lighttpd', '-D', '-f', join(path, "servers", "lighttpd.conf")] +            self.p = Popen(command, stderr=PIPE, stdin=PIPE, stdout=Output(out)) -            command2 = ['lighttpd', '-D', '-f', join(path, "servers", "lighttpd.conf")] -            self.p2 = Popen(command2, stderr=PIPE, stdin=PIPE, stdout=Output(out)) +            import run_fcgi +            run_fcgi.handle("daemonize=false", "method=threaded", "host=127.0.0.1", "port=9295")          elif self.server == "builtin":              self.pycore.logger.info("Starting django builtin Webserver: %s:%s" % (host, port)) -             -            command = ['python', join(self.pycore.path, "module", "web", "run_server.py"), "%s:%s" % (host, port)] -            self.p = Popen(command, stderr=Output(out), stdin=Output(out), stdout=Output(out)) + +            import run_server +            run_server.handle(host, port) +            #command = ['python', join(self.pycore.path, "module", "web", "run_server.py"), "%s:%s" % (host, port)] +            #self.p = Popen(command, stderr=Output(out), stdin=Output(out), stdout=Output(out))          else:              #run fastcgi on port -            command = ['python', join(self.pycore.path, "module", "web", "manage.py"), "runfcgi", "daemonize=false", "method=threaded", "host=127.0.0.1", "port=%s" % port] -            self.p = Popen(command, stderr=Output(out), stdin=PIPE, stdout=Output(out)) +            import run_fcgi +            run_fcgi.handle("daemonize=false", "method=threaded", "host=127.0.0.1", "port=%s" % str(port))      def quit(self):          try:              if self.server == "lighttpd" or self.server == "nginx":                  self.p.kill() -                self.p2.kill() +                #self.p2.kill()                  return True              else: -                self.p.kill() +                #self.p.kill()                  return True          except:              pass diff --git a/module/web/run_fcgi.py b/module/web/run_fcgi.py new file mode 100644 index 000000000..ce182f158 --- /dev/null +++ b/module/web/run_fcgi.py @@ -0,0 +1,172 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import sys + +from django.utils import importlib +from flup.server.fcgi import WSGIServer +from flup.server.fcgi_base import BaseFCGIServer +from flup.server.fcgi_base import FCGI_RESPONDER +from flup.server.threadedserver import ThreadedServer + + +os.environ["DJANGO_SETTINGS_MODULE"] = 'settings' + +def handle(*args, **options): +    from django.conf import settings +    from django.utils import translation +    # Activate the current language, because it won't get activated later. +    try: +        translation.activate(settings.LANGUAGE_CODE) +    except AttributeError: +        pass +    #from django.core.servers.fastcgi import runfastcgi +    runfastcgi(args) + + +FASTCGI_OPTIONS = { +    'protocol': 'fcgi', +    'host': None, +    'port': None, +    'socket': None, +    'method': 'fork', +    'daemonize': None, +    'workdir': '/', +    'pidfile': None, +    'maxspare': 5, +    'minspare': 2, +    'maxchildren': 50, +    'maxrequests': 0, +    'debug': None, +    'outlog': None, +    'errlog': None, +    'umask': None, +} + + +def runfastcgi(argset=[], **kwargs): +    options = FASTCGI_OPTIONS.copy() +    options.update(kwargs) +    for x in argset: +        if "=" in x: +            k, v = x.split('=', 1) +        else: +            k, v = x, True +        options[k.lower()] = v + +    try: +        import flup +    except ImportError, e: +        print >> sys.stderr, "ERROR: %s" % e +        print >> sys.stderr, "  Unable to load the flup package.  In order to run django" +        print >> sys.stderr, "  as a FastCGI application, you will need to get flup from" +        print >> sys.stderr, "  http://www.saddi.com/software/flup/   If you've already" +        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH." +        return False + +    flup_module = 'server.' + options['protocol'] + +    if options['method'] in ('prefork', 'fork'): +        wsgi_opts = { +            'maxSpare': int(options["maxspare"]), +            'minSpare': int(options["minspare"]), +            'maxChildren': int(options["maxchildren"]), +            'maxRequests': int(options["maxrequests"]), +        } +        flup_module += '_fork' +    elif options['method'] in ('thread', 'threaded'): +        wsgi_opts = { +            'maxSpare': int(options["maxspare"]), +            'minSpare': int(options["minspare"]), +            'maxThreads': int(options["maxchildren"]), +        } +    else: +        print "ERROR: Implementation must be one of prefork or thread." + +    wsgi_opts['debug'] = options['debug'] is not None + +    #try: +    #    module = importlib.import_module('.%s' % flup_module, 'flup') +    #    WSGIServer = module.WSGIServer +    #except: +    #    print "Can't import flup." + flup_module +    #    return False + +    # Prep up and go +    from django.core.handlers.wsgi import WSGIHandler + +    if options["host"] and options["port"] and not options["socket"]: +        wsgi_opts['bindAddress'] = (options["host"], int(options["port"])) +    elif options["socket"] and not options["host"] and not options["port"]: +        wsgi_opts['bindAddress'] = options["socket"] +    elif not options["socket"] and not options["host"] and not options["port"]: +        wsgi_opts['bindAddress'] = None +    else: +        return fastcgi_help("Invalid combination of host, port, socket.") + +    daemon_kwargs = {} +    if options['outlog']: +        daemon_kwargs['out_log'] = options['outlog'] +    if options['errlog']: +        daemon_kwargs['err_log'] = options['errlog'] +    if options['umask']: +        daemon_kwargs['umask'] = int(options['umask']) + +    ownWSGIServer(WSGIHandler(), **wsgi_opts).run() + +class ownThreadedServer(ThreadedServer): +    def _installSignalHandlers(self): +        return + +    def _restoreSignalHandlers(self): +        return + + +class ownWSGIServer(BaseFCGIServer, ownThreadedServer): + +    def __init__(self, application, environ=None, +                 multithreaded=True, multiprocess=False, +                 bindAddress=None, umask=None, multiplexed=False, +                 debug=True, roles=(FCGI_RESPONDER,), forceCGI=False, **kw): +        BaseFCGIServer.__init__(self, application, +                                environ=environ, +                                multithreaded=multithreaded, +                                multiprocess=multiprocess, +                                bindAddress=bindAddress, +                                umask=umask, +                                multiplexed=multiplexed, +                                debug=debug, +                                roles=roles, +                                forceCGI=forceCGI) +        for key in ('jobClass', 'jobArgs'): +            if kw.has_key(key): +                del kw[key] +        ownThreadedServer.__init__(self, jobClass=self._connectionClass, +                                jobArgs=(self,), **kw) + +    def _isClientAllowed(self, addr): +        return self._web_server_addrs is None or \ +               (len(addr) == 2 and addr[0] in self._web_server_addrs) + +    def run(self): +        """ +        The main loop. Exits on SIGHUP, SIGINT, SIGTERM. Returns True if +        SIGHUP was received, False otherwise. +        """ +        self._web_server_addrs = os.environ.get('FCGI_WEB_SERVER_ADDRS') +        if self._web_server_addrs is not None: +            self._web_server_addrs = map(lambda x: x.strip(), +                                         self._web_server_addrs.split(',')) + +        sock = self._setupSocket() + +        ret = ownThreadedServer.run(self, sock) + +        self._cleanupSocket(sock) + +        return ret + +if __name__ == "__main__": +    handle(*sys.argv[1:]) + diff --git a/module/web/run_server.py b/module/web/run_server.py index 5ffa45781..6aa8bdba4 100755 --- a/module/web/run_server.py +++ b/module/web/run_server.py @@ -2,6 +2,9 @@  # -*- coding: utf-8 -*-  import os  import sys +import django +from django.core.servers.basehttp import AdminMediaHandler, WSGIServerException, WSGIServer, WSGIRequestHandler +from django.core.handlers.wsgi import WSGIHandler  os.environ["DJANGO_SETTINGS_MODULE"] = 'settings' @@ -15,14 +18,10 @@ class Output:       def __getattr__(self, attr):          return getattr(self.stream, attr) -sys.stderr = Output(sys.stderr) +#sys.stderr = Output(sys.stderr)  #sys.stdout = Output(sys.stdout)  def handle(* args): -    import django -    from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException -    from django.core.handlers.wsgi import WSGIHandler -          try:  	if len(args) == 1:  	    try: @@ -37,7 +36,7 @@ def handle(* args):  	addr = '127.0.0.1'  	port = '8000' -    print addr, port +    #print addr, port      admin_media_path = ''      shutdown_message = '' @@ -46,9 +45,9 @@ def handle(* args):      from django.conf import settings      from django.utils import translation -    print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE) +    print "Django version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)      print "Development server is running at http://%s:%s/" % (addr, port) -    print "Quit the server with %s." % quit_command +    #print "Quit the server with %s." % quit_command      translation.activate(settings.LANGUAGE_CODE) @@ -74,7 +73,17 @@ def handle(* args):          if shutdown_message:              print shutdown_message          sys.exit(0) -         + +class ownRequestHandler(WSGIRequestHandler): +    def log_message(self, format, *args): +        return + + +def run(addr, port, wsgi_handler): +    server_address = (addr, port) +    httpd = WSGIServer(server_address, ownRequestHandler) +    httpd.set_app(wsgi_handler) +    httpd.serve_forever()  if __name__ == "__main__":      handle(*sys.argv[1:]) | 
