Login

Run Django as a FastCGI authorizer

Author:
cme
Posted:
October 23, 2008
Language:
Python
Version:
1.0
Tags:
authenticate fcgi fastcgi
Score:
0 (after 0 ratings)

I use this as the FastCGI script for authorizers with lighttpd (though I guess it should work with little change on any other webserver supporting FastCGI). I point it to the same Django project/settings as the normal responder script.

As I use it to gate access to pages not served by Django, I can include those non-Django URLs in the main urls.py, connected to special authorizer view functions (in another snippet).

The two key parts of the script, compared to the equivalent one for Django in the normal FastCGI Responder role, are:

  1. Pass the FCGI_AUTHORIZER as the role to WSGIServer
  2. Generate a PATH_INFO variable from the REQUEST_URI (FastCGI authorizers aren't given PATH_INFO, but Django needs that to match against the URLconf.)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python
import sys, os

# Set the DJANGO_SETTINGS_MODULE environment variable.
# Customise this for your setup
os.environ['DJANGO_SETTINGS_MODULE'] = "djsite.settings"

from django.core.handlers.wsgi import WSGIHandler
main_handler = WSGIHandler()

def wrapper(environ, start_response):
    """FastCGI Authorizers don't get PATH_INFO.  Make one from
    REQUEST_URI so that Django can use it in URL matching."""
    if not environ["PATH_INFO"]:
        # Fastcgi doesn't give you PATH_INFO for authorizers
        uri = environ['REQUEST_URI']
        if '?' in uri:
            path, query = environ['REQUEST_URI'].split('?', 1)
        else:
            path, query = uri, ''
        environ['PATH_INFO'] = path

    return main_handler(environ, start_response)

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer
    from flup.server.fcgi_base import FCGI_AUTHORIZER

    # Django normally uses the FCGI_RESPONDER role and will reject AUTHORIZER
    # requests.
    WSGIServer(wrapper, roles=(FCGI_AUTHORIZER,)).run()

More like this

  1. Simple FastCGI authorizer view by cme 6 years, 5 months ago
  2. autotranslate po files using microsoft translator by prabhat246 3 years, 1 month ago
  3. Mobilize your Django site by stevena0 5 years, 11 months ago
  4. Mod to allow simple_tag to access context by leaf 6 years, 6 months ago
  5. Command to dump data as a python script by willhardy 6 years, 9 months ago

Comments

Please login first before commenting.