# HG changeset patch # User paulb # Date 1102005609 0 # Node ID 892fa95365763899612f19a35b668dc56c476365 # Parent fc6716d682762201d5ffb5ed1b0afdc56f8dfc29 [project @ 2004-12-02 16:39:58 by paulb] Changed the example WSGI environment to the run_with_cgi function provided in PEP 333 (since wsgiServer seemed to have issues with request processing, particularly POST methods). diff -r fc6716d68276 -r 892fa9536576 docs/WSGI/NOTES.txt --- a/docs/WSGI/NOTES.txt Thu Dec 02 16:39:50 2004 +0000 +++ b/docs/WSGI/NOTES.txt Thu Dec 02 16:40:09 2004 +0000 @@ -1,11 +1,19 @@ -Specifying the appropriate PYTHONPATH, invoke the application program. For -example, in the WebStack distribution directory: +Currently, the WSGI examples use the WSGI CGI handler code found in PEP 333 +and placed in the wsgi_cgi module. Deploying in other environments would +require slightly different "glue code" to be written. -PYTHONPATH=.:examples/Common python examples/WSGI/SimpleApp.py +-------- + +Declare the script in httpd.conf or equivalent Web server configuration file. +For example: + +ScriptAlias /wsgi/simple "/home/paulb/Software/Python/WebStack/examples/WSGI/SimpleHandler.py" The WebStack package must reside on the PYTHONPATH, along with the package -containing the application itself. +containing the application itself. Therefore, ensure that the handler uses the +appropriate entries in sys.path. -In addition, WSGI examples currently require wsgiServer from WSGI Utils to -either reside on the PYTHONPATH or to have been installed using the WSGI -Utils setup script. +Ensure that the handler file for the application has the appropriate +permissions: + +chmod u+x examples/WSGI/SimpleHandler.py diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/CalendarApp.py --- a/examples/WSGI/CalendarApp.py Thu Dec 02 16:39:50 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#!/usr/bin/env python - -from WebStack.Adapters import WSGI -from Calendar import DirectoryResource -from wsgiutils import wsgiServer, wsgiAdaptor - -# Magic dictionary for WSGIServer. - -class MagicDict: - def __init__(self, handler): - self.handler = handler - def has_key(self, name): - return 1 - def __getitem__(self, name): - return self.handler - -# Special magic incantation. - -handler = WSGI.WSGIAdapter(DirectoryResource()) -address = ("", 8080) -server = wsgiServer.WSGIServer(address, MagicDict(handler)) -print "Serving..." -server.serve_forever() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/CalendarHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/CalendarHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from Calendar import DirectoryResource +from wsgi_cgi import run_with_cgi + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(DirectoryResource()) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/CookiesApp.py --- a/examples/WSGI/CookiesApp.py Thu Dec 02 16:39:50 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#!/usr/bin/env python - -from WebStack.Adapters import WSGI -from Cookies import CookiesResource -from wsgiutils import wsgiServer, wsgiAdaptor - -# Magic dictionary for WSGIServer. - -class MagicDict: - def __init__(self, handler): - self.handler = handler - def has_key(self, name): - return 1 - def __getitem__(self, name): - return self.handler - -# Special magic incantation. - -handler = WSGI.WSGIAdapter(CookiesResource()) -address = ("", 8080) -server = wsgiServer.WSGIServer(address, MagicDict(handler)) -print "Serving..." -server.serve_forever() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/CookiesHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/CookiesHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from Cookies import CookiesResource +from wsgi_cgi import run_with_cgi + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(CookiesResource()) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/FormHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/FormHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from Form import FormResource +from wsgi_cgi import run_with_cgi + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(FormResource()) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/LoginHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/LoginHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from WebStack.Resources.Login import LoginResource, LoginAuthenticator +from wsgi_cgi import run_with_cgi + +resource = LoginResource( + LoginAuthenticator( + secret_key="horses", + credentials=( + ("badger", "abc"), + ("vole", "xyz"), + ) + ) +) + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(resource) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/SessionsHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/SessionsHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from Sessions import SessionsResource +from wsgi_cgi import run_with_cgi + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(SessionsResource()) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/SimpleApp.py --- a/examples/WSGI/SimpleApp.py Thu Dec 02 16:39:50 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#!/usr/bin/env python - -from WebStack.Adapters import WSGI -from Simple import SimpleResource -from wsgiutils import wsgiServer, wsgiAdaptor - -# Magic dictionary for WSGIServer. - -class MagicDict: - def __init__(self, handler): - self.handler = handler - def has_key(self, name): - return 1 - def __getitem__(self, name): - return self.handler - -# Special magic incantation. - -handler = WSGI.WSGIAdapter(SimpleResource()) -address = ("", 8080) -server = wsgiServer.WSGIServer(address, MagicDict(handler)) -print "Serving..." -server.serve_forever() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/SimpleHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/SimpleHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from Simple import SimpleResource +from wsgi_cgi import run_with_cgi + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(SimpleResource()) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/SimpleWithLoginHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/SimpleWithLoginHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from WebStack.Resources.LoginRedirect import LoginRedirectResource, LoginRedirectAuthenticator +from Simple import SimpleResource +from wsgi_cgi import run_with_cgi + +resource = LoginRedirectResource( + login_url="http://localhost/wsgi/login", + app_url="http://localhost", + resource=SimpleResource(), + authenticator=LoginRedirectAuthenticator(secret_key="horses"), + anonymous_parameter_name="anonymous", + logout_parameter_name="logout" +) + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(resource) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/UnicodeHandler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/UnicodeHandler.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +# NOTE: Path manipulation requires manual customisation. + +import sys +sys.path.append("/home/paulb/Software/Python/WebStack") +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") + +from WebStack.Adapters import WSGI +from Unicode import UnicodeResource +from wsgi_cgi import run_with_cgi + +# Special magic incantation. + +handler = WSGI.WSGIAdapter(UnicodeResource()) +run_with_cgi(handler) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r fc6716d68276 -r 892fa9536576 examples/WSGI/wsgi_cgi.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/WSGI/wsgi_cgi.py Thu Dec 02 16:40:09 2004 +0000 @@ -0,0 +1,69 @@ +#!/usr/local/bin/python + +""" +WSGI CGI handler code originating from PEP 333, transcribed by Titus Brown. +Previously a standalone CGI program, this is now a module imported by the actual +CGI programs. +""" + +import os, sys + +def run_with_cgi(application): + + environ = dict(os.environ.items()) + environ['wsgi.input'] = sys.stdin + environ['wsgi.errors'] = sys.stderr + environ['wsgi.version'] = (1,0) + environ['wsgi.multithread'] = False + environ['wsgi.multiprocess'] = True + environ['wsgi.run_once'] = True + + if environ.get('HTTPS','off') in ('on','1'): + environ['wsgi.url_scheme'] = 'https' + else: + environ['wsgi.url_scheme'] = 'http' + + headers_set = [] + headers_sent = [] + + def write(data): + if not headers_set: + raise AssertionError("write() before start_response()") + + elif not headers_sent: + # Before the first output, send the stored headers + status, response_headers = headers_sent[:] = headers_set + sys.stdout.write('Status: %s\r\n' % status) + for header in response_headers: + sys.stdout.write('%s: %s\r\n' % header) + sys.stdout.write('\r\n') + + sys.stdout.write(data) + sys.stdout.flush() + + def start_response(status,response_headers,exc_info=None): + if exc_info: + try: + if headers_sent: + # Re-raise original exception if headers sent + raise exc_info[0], exc_info[1], exc_info[2] + finally: + exc_info = None # avoid dangling circular ref + elif headers_set: + raise AssertionError("Headers already set!") + + headers_set[:] = [status,response_headers] + return write + + result = application(environ, start_response) + try: + for data in result: + if data: # don't send headers until body appears + write(data) + if not headers_sent: + write('') # send headers now if body was empty + finally: + if hasattr(result,'close'): + result.close() + +# vim: tabstop=4 expandtab shiftwidth=4