2006-06-20 13:24:19 +08:00
|
|
|
"""
|
2007-04-01 15:30:27 +08:00
|
|
|
FastCGI (or SCGI, or AJP1.3 ...) server that implements the WSGI protocol.
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
Uses the flup python package: http://www.saddi.com/software/flup/
|
|
|
|
|
|
|
|
This is a adaptation of the flup package to add FastCGI server support
|
|
|
|
to run Django apps from Web servers that support the FastCGI protocol.
|
|
|
|
This module can be run standalone or from the django-admin / manage.py
|
|
|
|
scripts using the "runfcgi" directive.
|
|
|
|
|
|
|
|
Run with the extra option "help" for a list of additional options you can
|
|
|
|
pass to this server.
|
|
|
|
"""
|
|
|
|
|
2013-07-29 21:50:58 +08:00
|
|
|
import importlib
|
2011-07-13 17:35:51 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
__version__ = "0.1"
|
|
|
|
__all__ = ["runfastcgi"]
|
|
|
|
|
2012-01-03 03:20:47 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2007-09-10 05:57:59 +08:00
|
|
|
FASTCGI_HELP = r"""
|
2007-04-01 15:30:27 +08:00
|
|
|
Run this project as a fastcgi (or some other protocol supported
|
|
|
|
by flup) application. To do this, the flup package from
|
|
|
|
http://www.saddi.com/software/flup/ is required.
|
2006-06-20 13:24:19 +08:00
|
|
|
|
2007-09-10 05:57:59 +08:00
|
|
|
runfcgi [options] [fcgi settings]
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
Optional Fcgi settings: (setting=value)
|
2012-01-03 03:20:47 +08:00
|
|
|
protocol=PROTOCOL fcgi, scgi, ajp, ... (default %(protocol)s)
|
2010-10-27 23:12:13 +08:00
|
|
|
host=HOSTNAME hostname to listen on.
|
2006-06-20 13:24:19 +08:00
|
|
|
port=PORTNUM port to listen on.
|
|
|
|
socket=FILE UNIX socket to listen on.
|
2012-01-03 03:20:47 +08:00
|
|
|
method=IMPL prefork or threaded (default %(method)s).
|
2010-10-27 23:12:13 +08:00
|
|
|
maxrequests=NUMBER number of requests a child handles before it is
|
2006-10-31 20:27:55 +08:00
|
|
|
killed and a new child is forked (0 = no limit).
|
2012-01-03 03:20:47 +08:00
|
|
|
maxspare=NUMBER max number of spare processes / threads (default %(maxspare)s).
|
|
|
|
minspare=NUMBER min number of spare processes / threads (default %(minspare)s).
|
|
|
|
maxchildren=NUMBER hard limit number of processes / threads (default %(maxchildren)s).
|
2006-06-20 13:24:19 +08:00
|
|
|
daemonize=BOOL whether to detach from terminal.
|
|
|
|
pidfile=FILE write the spawned process-id to this file.
|
2012-01-03 03:20:47 +08:00
|
|
|
workdir=DIRECTORY change to this directory when daemonizing (default %(workdir)s).
|
2010-10-27 23:12:13 +08:00
|
|
|
debug=BOOL set to true to enable flup tracebacks.
|
2008-03-18 23:53:46 +08:00
|
|
|
outlog=FILE write stdout to this file.
|
|
|
|
errlog=FILE write stderr to this file.
|
2010-10-27 23:12:13 +08:00
|
|
|
umask=UMASK umask to use when daemonizing, in octal notation (default 022).
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
Run a "standard" fastcgi process on a file-descriptor
|
2010-10-09 16:12:50 +08:00
|
|
|
(for Web servers which spawn your processes for you)
|
2006-06-20 13:24:19 +08:00
|
|
|
$ manage.py runfcgi method=threaded
|
|
|
|
|
2007-04-01 15:30:27 +08:00
|
|
|
Run a scgi server on a TCP host/port
|
|
|
|
$ manage.py runfcgi protocol=scgi method=prefork host=127.0.0.1 port=8025
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
Run a fastcgi server on a UNIX domain socket (posix platforms only)
|
|
|
|
$ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock
|
|
|
|
|
|
|
|
Run a fastCGI as a daemon and write the spawned PID in a file
|
|
|
|
$ manage.py runfcgi socket=/tmp/fcgi.sock method=prefork \
|
|
|
|
daemonize=true pidfile=/var/run/django-fcgi.pid
|
|
|
|
|
2012-01-03 03:20:47 +08:00
|
|
|
""" % FASTCGI_OPTIONS
|
2006-06-20 13:24:19 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-06-20 13:24:19 +08:00
|
|
|
def fastcgi_help(message=None):
|
2012-04-29 00:02:01 +08:00
|
|
|
print(FASTCGI_HELP)
|
2006-06-20 13:24:19 +08:00
|
|
|
if message:
|
2012-04-29 00:02:01 +08:00
|
|
|
print(message)
|
2006-06-20 13:24:19 +08:00
|
|
|
return False
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-09-21 21:45:24 +08:00
|
|
|
def runfastcgi(argset=[], **kwargs):
|
2006-06-20 13:24:19 +08:00
|
|
|
options = FASTCGI_OPTIONS.copy()
|
2006-09-14 23:37:11 +08:00
|
|
|
options.update(kwargs)
|
2006-06-20 13:24:19 +08:00
|
|
|
for x in argset:
|
|
|
|
if "=" in x:
|
|
|
|
k, v = x.split('=', 1)
|
|
|
|
else:
|
|
|
|
k, v = x, True
|
|
|
|
options[k.lower()] = v
|
|
|
|
|
|
|
|
if "help" in options:
|
|
|
|
return fastcgi_help()
|
|
|
|
|
|
|
|
try:
|
2013-10-18 19:25:30 +08:00
|
|
|
import flup # NOQA
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2012-04-29 00:02:01 +08:00
|
|
|
sys.stderr.write("ERROR: %s\n" % e)
|
|
|
|
sys.stderr.write(" Unable to load the flup package. In order to run django\n")
|
|
|
|
sys.stderr.write(" as a FastCGI application, you will need to get flup from\n")
|
|
|
|
sys.stderr.write(" http://www.saddi.com/software/flup/ If you've already\n")
|
|
|
|
sys.stderr.write(" installed flup, then make sure you have it in your PYTHONPATH.\n")
|
2006-06-20 13:24:19 +08:00
|
|
|
return False
|
|
|
|
|
2007-04-01 15:30:27 +08:00
|
|
|
flup_module = 'server.' + options['protocol']
|
|
|
|
|
2006-06-20 13:24:19 +08:00
|
|
|
if options['method'] in ('prefork', 'fork'):
|
|
|
|
wsgi_opts = {
|
|
|
|
'maxSpare': int(options["maxspare"]),
|
|
|
|
'minSpare': int(options["minspare"]),
|
|
|
|
'maxChildren': int(options["maxchildren"]),
|
2009-03-19 00:55:59 +08:00
|
|
|
'maxRequests': int(options["maxrequests"]),
|
2006-06-20 13:24:19 +08:00
|
|
|
}
|
2007-04-01 15:30:27 +08:00
|
|
|
flup_module += '_fork'
|
2006-06-20 13:24:19 +08:00
|
|
|
elif options['method'] in ('thread', 'threaded'):
|
2006-11-07 10:01:16 +08:00
|
|
|
wsgi_opts = {
|
|
|
|
'maxSpare': int(options["maxspare"]),
|
|
|
|
'minSpare': int(options["minspare"]),
|
|
|
|
'maxThreads': int(options["maxchildren"]),
|
|
|
|
}
|
2006-06-20 13:24:19 +08:00
|
|
|
else:
|
2012-01-03 03:20:47 +08:00
|
|
|
return fastcgi_help("ERROR: Implementation must be one of prefork or "
|
|
|
|
"thread.")
|
2006-06-20 13:24:19 +08:00
|
|
|
|
2009-04-02 05:37:34 +08:00
|
|
|
wsgi_opts['debug'] = options['debug'] is not None
|
2006-12-07 11:04:22 +08:00
|
|
|
|
2007-04-01 15:30:27 +08:00
|
|
|
try:
|
2009-03-20 09:43:35 +08:00
|
|
|
module = importlib.import_module('.%s' % flup_module, 'flup')
|
2009-03-19 00:55:59 +08:00
|
|
|
WSGIServer = module.WSGIServer
|
2012-01-03 03:20:47 +08:00
|
|
|
except Exception:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Can't import flup." + flup_module)
|
2007-04-01 15:30:27 +08:00
|
|
|
return False
|
|
|
|
|
2006-06-20 13:24:19 +08:00
|
|
|
# Prep up and go
|
2011-10-22 12:30:10 +08:00
|
|
|
from django.core.servers.basehttp import get_internal_wsgi_application
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
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.")
|
|
|
|
|
|
|
|
if options["daemonize"] is None:
|
|
|
|
# Default to daemonizing if we're running on a socket/named pipe.
|
|
|
|
daemonize = (wsgi_opts['bindAddress'] is not None)
|
|
|
|
else:
|
|
|
|
if options["daemonize"].lower() in ('true', 'yes', 't'):
|
|
|
|
daemonize = True
|
|
|
|
elif options["daemonize"].lower() in ('false', 'no', 'f'):
|
|
|
|
daemonize = False
|
|
|
|
else:
|
2012-01-03 03:20:47 +08:00
|
|
|
return fastcgi_help("ERROR: Invalid option for daemonize "
|
|
|
|
"parameter.")
|
2006-06-20 13:24:19 +08:00
|
|
|
|
2008-03-18 23:53:46 +08:00
|
|
|
daemon_kwargs = {}
|
|
|
|
if options['outlog']:
|
|
|
|
daemon_kwargs['out_log'] = options['outlog']
|
|
|
|
if options['errlog']:
|
|
|
|
daemon_kwargs['err_log'] = options['errlog']
|
2008-06-30 19:22:41 +08:00
|
|
|
if options['umask']:
|
2010-10-27 23:12:13 +08:00
|
|
|
daemon_kwargs['umask'] = int(options['umask'], 8)
|
2008-03-18 23:53:46 +08:00
|
|
|
|
2006-06-20 13:24:19 +08:00
|
|
|
if daemonize:
|
|
|
|
from django.utils.daemonize import become_daemon
|
2008-03-18 23:53:46 +08:00
|
|
|
become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
if options["pidfile"]:
|
2012-05-05 20:01:38 +08:00
|
|
|
with open(options["pidfile"], "w") as fp:
|
|
|
|
fp.write("%d\n" % os.getpid())
|
2006-06-20 13:24:19 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
WSGIServer(get_internal_wsgi_application(), **wsgi_opts).run()
|
2006-06-20 13:24:19 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
runfastcgi(sys.argv[1:])
|