Created
August 17, 2010 15:57
-
-
Save qtip/530525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2010 Daniel Snider <qtipkk@gmail.com> | |
# | |
"""Django and WSGI ISAPI request handler. | |
Make a new application pool for each project and have an IIS Application point to a directory | |
containing either a .wsgi file, or a django project with a settings.py | |
""" | |
import sys | |
from Http.WSGI import RunWSGI | |
from Http import Env | |
import os | |
from PyISAPIe import Write | |
def path_module(path): | |
"""Split a path into containing path and folder.""" | |
pth, module = os.path.split(path) | |
if not module: | |
return os.path.split(pth) | |
else: | |
return pth, module | |
def wsgi_app(root_dir): | |
"""Get the handler from a .wsgi file in the root_dir.""" | |
for path in os.listdir(root_dir): | |
if path.endswith(".wsgi"): | |
variables = {} | |
execfile(os.path.join(root_dir, path), variables) | |
if variables.get("application"): | |
return variables["application"] | |
raise Exception("Couldn't find a WSGI application") | |
def django_app(root_dir): | |
"""Get the handler from a django project file in the root_dir.""" | |
import django.core.handlers.wsgi | |
return django.core.handlers.wsgi.WSGIHandler() | |
def get_wsgi_application(root_dir): | |
"""Get either a WSGI app or a Django app.""" | |
try: | |
return wsgi_app(root_dir) | |
except: | |
from django.core.handlers.wsgi import WSGIHandler as DjangoHandler | |
if os.path.exists(os.path.join(root_dir, "settings.py")): | |
path, module = path_module(Env.APPL_PHYSICAL_PATH) | |
sys.path.append(path) | |
os.environ["DJANGO_SETTINGS_MODULE"] = module+'.settings' | |
return django_app(root_dir) | |
else: | |
raise Exception("Couldn't find a WSGI application or Django application") | |
handler = None | |
# PyISAPIe request | |
def Request(): | |
global handler | |
if not handler: | |
handler = get_wsgi_application(Env.APPL_PHYSICAL_PATH) | |
s = Env.APPL_MD_PATH.split('/') | |
base = '/'+'/'.join(s[s.index('ROOT')+1:]) | |
return RunWSGI(handler, Base=base) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment