| 1 |
#!/usr/bin/env python |
|---|
| 2 |
# encoding: utf-8 |
|---|
| 3 |
""" |
|---|
| 4 |
django.cgi |
|---|
| 5 |
|
|---|
| 6 |
A simple cgi script which uses the django WSGI to serve requests. |
|---|
| 7 |
|
|---|
| 8 |
Code copy/pasted from PEP-0333 and then tweaked to serve django. |
|---|
| 9 |
http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side |
|---|
| 10 |
|
|---|
| 11 |
This script assumes django is on your sys.path, and that your site code is at |
|---|
| 12 |
/home/mycode/mysite. Copy this script into your cgi-bin directory (or do |
|---|
| 13 |
whatever you need to to make a cgi script executable on your system), and then |
|---|
| 14 |
update the paths at the bottom of this file to suit your site. |
|---|
| 15 |
|
|---|
| 16 |
This is probably the slowest way to serve django pages, as the python |
|---|
| 17 |
interpreter, the django code-base and your site code has to be loaded every |
|---|
| 18 |
time a request is served. FCGI and mod_python solve this problem, use them if |
|---|
| 19 |
you can. |
|---|
| 20 |
|
|---|
| 21 |
In order to speed things up it may be worth experimenting with running |
|---|
| 22 |
uncompressed zips on the sys.path for django and the site code, as this can be |
|---|
| 23 |
(theorectically) faster. See PEP-0273 (specifically Benchmarks). |
|---|
| 24 |
http://www.python.org/dev/peps/pep-0273/ |
|---|
| 25 |
|
|---|
| 26 |
Make sure all python files are compiled in your code base. See |
|---|
| 27 |
http://docs.python.org/lib/module-compileall.html |
|---|
| 28 |
|
|---|
| 29 |
""" |
|---|
| 30 |
|
|---|
| 31 |
import os, sys |
|---|
| 32 |
# insert a sys.path.append("whatever") in here if django is not |
|---|
| 33 |
# on your sys.path. |
|---|
| 34 |
import django.core.handlers.wsgi |
|---|
| 35 |
|
|---|
| 36 |
def run_with_cgi(application): |
|---|
| 37 |
|
|---|
| 38 |
environ = dict(os.environ.items()) |
|---|
| 39 |
environ['wsgi.input'] = sys.stdin |
|---|
| 40 |
environ['wsgi.errors'] = sys.stderr |
|---|
| 41 |
environ['wsgi.version'] = (1,0) |
|---|
| 42 |
environ['wsgi.multithread'] = False |
|---|
| 43 |
environ['wsgi.multiprocess'] = True |
|---|
| 44 |
environ['wsgi.run_once'] = True |
|---|
| 45 |
|
|---|
| 46 |
if environ.get('HTTPS','off') in ('on','1'): |
|---|
| 47 |
environ['wsgi.url_scheme'] = 'https' |
|---|
| 48 |
else: |
|---|
| 49 |
environ['wsgi.url_scheme'] = 'http' |
|---|
| 50 |
|
|---|
| 51 |
headers_set = [] |
|---|
| 52 |
headers_sent = [] |
|---|
| 53 |
|
|---|
| 54 |
def write(data): |
|---|
| 55 |
if not headers_set: |
|---|
| 56 |
raise AssertionError("write() before start_response()") |
|---|
| 57 |
|
|---|
| 58 |
elif not headers_sent: |
|---|
| 59 |
# Before the first output, send the stored headers |
|---|
| 60 |
status, response_headers = headers_sent[:] = headers_set |
|---|
| 61 |
sys.stdout.write('Status: %s\r\n' % status) |
|---|
| 62 |
for header in response_headers: |
|---|
| 63 |
sys.stdout.write('%s: %s\r\n' % header) |
|---|
| 64 |
sys.stdout.write('\r\n') |
|---|
| 65 |
|
|---|
| 66 |
sys.stdout.write(data) |
|---|
| 67 |
sys.stdout.flush() |
|---|
| 68 |
|
|---|
| 69 |
def start_response(status,response_headers,exc_info=None): |
|---|
| 70 |
if exc_info: |
|---|
| 71 |
try: |
|---|
| 72 |
if headers_sent: |
|---|
| 73 |
# Re-raise original exception if headers sent |
|---|
| 74 |
raise exc_info[0], exc_info[1], exc_info[2] |
|---|
| 75 |
finally: |
|---|
| 76 |
exc_info = None # avoid dangling circular ref |
|---|
| 77 |
elif headers_set: |
|---|
| 78 |
raise AssertionError("Headers already set!") |
|---|
| 79 |
|
|---|
| 80 |
headers_set[:] = [status,response_headers] |
|---|
| 81 |
return write |
|---|
| 82 |
|
|---|
| 83 |
result = application(environ, start_response) |
|---|
| 84 |
try: |
|---|
| 85 |
for data in result: |
|---|
| 86 |
if data: # don't send headers until body appears |
|---|
| 87 |
write(data) |
|---|
| 88 |
if not headers_sent: |
|---|
| 89 |
write('') # send headers now if body was empty |
|---|
| 90 |
finally: |
|---|
| 91 |
if hasattr(result,'close'): |
|---|
| 92 |
result.close() |
|---|
| 93 |
|
|---|
| 94 |
# Change this to the directory above your site code. |
|---|
| 95 |
sys.path.append("/home/mycode") |
|---|
| 96 |
# Change mysite to the name of your site package |
|---|
| 97 |
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' |
|---|
| 98 |
run_with_cgi(django.core.handlers.wsgi.WSGIHandler()) |
|---|