jamiejackson

Untitled

Jul 21st, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. # $URL: https://pyisapie.svn.sourceforge.net/svnroot/pyisapie/Tags/1.1.0-rc4/PyISAPIe/Python/Examples/WSGI/Isapi.py $
  2. # $Rev: 95 $ $Date: 2008-01-11 11:07:35 -0800 (Fri, 11 Jan 2008) $
  3. # (C)2008 Phillip Sitbon <[email protected]>
  4. #
  5. """Global ISAPI request handler for use with WSGI applications.
  6.  
  7. This example merges two WSGI applications and also runs scripts
  8. directly.
  9.  
  10. If you don't have either Django or Trac, comment out that section
  11. and the entry in "Apps" and it should work fine.
  12.  
  13. You may also believe that you can run multiple Django projects just
  14. by choosing the DJANGO_SETTINGS_MODULE environment variable based
  15. on the URL, but beware- that is a process-level value you're
  16. changing! I'm not sure if the value is also looked for in the WSGI
  17. environment. For now, I'm just leaving it at one project.
  18.  
  19. I chose to place the root folder under /app for the specific
  20. purpose of making PyISAPIe the wildcard handler for that path only.
  21. For IIS 5.x, all the paths must end in ".py" because an extension
  22. must be associated with a handler.
  23.  
  24. See the install docs for further information, and the WSGI setup
  25. page for specifics about getting this script going.
  26. """
  27. from Http.WSGI import RunWSGI
  28. from Http import Env
  29. from md5 import md5
  30. import imp
  31. import os
  32.  
  33. #
  34. # Django rev 7002
  35. #
  36. # Due to the nature of this particular script, all the urls in
  37. # Django must start with /app/django/, or whatever you specify
  38. # below.
  39. #
  40. # from django.core.handlers.wsgi import WSGIHandler as DjangoHandler
  41. # os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.settings"
  42.  
  43. #
  44. # Trac 0.10.4 (worked, but could not fully verify)
  45. #
  46. # Aside from other issues, I wasn't patient enough to figure
  47. # out how to specify a base URL for Trac. In general, it
  48. # appeared to work.
  49. #
  50. # from trac.web.main import dispatch_request as TracHandler
  51. # os.environ["TRAC_ENV_PARENT_DIR"] = r"C:\Path\To\Trac" # or TRAC_ENV
  52.  
  53. #
  54. # Simple script handling.
  55. #
  56. # Just a copy of what's in the Request() of the default handler.
  57. #
  58. # If of course assumes that the file specified by the URL
  59. # is valid.
  60. #
  61.  
  62. from flask import Flask
  63. app = Flask(__name__)
  64. app.debug = True
  65.  
  66. ScriptHandlers = {}
  67. def RunScript(Path):
  68.   global ScriptHandlers
  69.   try:
  70.     # attempt to call an already-loaded request function.
  71.     return ScriptHandlers[Path]()
  72.   except KeyError:
  73.     # uses the script path's md5 hash to ensure a unique
  74.     # name - not the best way to do it, but it keeps
  75.     # undesired characters out of the name that will
  76.     # mess up the loading.
  77.     Name = '__'+md5(Path).hexdigest().upper()
  78.     ScriptHandlers[Path] = \
  79.       imp.load_source(Name, Env.SCRIPT_TRANSLATED).Request
  80.     return ScriptHandlers[Path]()
  81.  
  82. #
  83. # URL prefixes to map to the roots of each application.
  84. #
  85. Apps = {
  86. #  "/app/django/"  : lambda P: RunWSGI(DjangoHandler()),
  87. #  "/app/trac/"    : lambda P: RunWSGI(TracHandler),
  88. #  "/app/scripts/" : lambda P: RunScript(P),
  89.     "/app/scripts/" : lambda P: RunWSGI(app),
  90. }
  91.  
  92. #
  93. # The main request handler.
  94. #
  95. def Request():
  96.   # Might be better to do some caching here?
  97.   Name = Env.SCRIPT_NAME
  98.  
  99.   # Apps might be better off as a tuple-of-tuples,
  100.   # but for the sake of representation I leave it
  101.   # as a dict.
  102.   for App, Handler in Apps.items():
  103.     if Name.startswith(App):
  104.       return Handler(Name)
  105.  
  106.   # Cause 500 error: there should be a 404 handler, eh?
  107.   #
  108.   raise Exception, "Handler not found."
Advertisement
Add Comment
Please, Sign In to add comment