summaryrefslogtreecommitdiffstats
path: root/module/web/run_fcgi.py
diff options
context:
space:
mode:
authorGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2010-04-02 14:59:12 +0200
committerGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2010-04-02 14:59:12 +0200
commitae8f974c33ae5f4674e7812b339d04135e2e6b9f (patch)
treee664525b544e64b1237f8208de4a385e16770152 /module/web/run_fcgi.py
parentparser fix (diff)
downloadpyload-ae8f974c33ae5f4674e7812b339d04135e2e6b9f.tar.xz
django embedded
Diffstat (limited to 'module/web/run_fcgi.py')
-rw-r--r--module/web/run_fcgi.py172
1 files changed, 172 insertions, 0 deletions
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:])
+