From: Fredrik Tolf Date: Sun, 20 May 2012 02:31:52 +0000 (+0200) Subject: examples: Added an example for wsgidir usage. X-Git-Tag: 0.11~19 X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=commitdiff_plain;h=8774cda21dfafd73df99686ded102c4f6f5e2d2e examples: Added an example for wsgidir usage. --- diff --git a/examples/python/wsgidir/README b/examples/python/wsgidir/README new file mode 100644 index 0000000..6477756 --- /dev/null +++ b/examples/python/wsgidir/README @@ -0,0 +1,6 @@ +This example demonstrates how the shipped Python module ashd.wsgidir +can be used to call WSGI scripts in dirplex-served directories as +easily as PHP scripts or mod_python scripts under Apache. + +To try this example, run it on your local machine and request + or . diff --git a/examples/python/wsgidir/index.wsgi b/examples/python/wsgidir/index.wsgi new file mode 100644 index 0000000..166241c --- /dev/null +++ b/examples/python/wsgidir/index.wsgi @@ -0,0 +1,29 @@ +import sys, ashd.wsgiutil + +n = 0 + +def application(env, startreq): + global n + startreq("200 OK", [("Content-Type", "text/html")]) + yield "\n" + yield "Hello world from Python!\n" + yield "\n" + yield "

Hello world from Python/WSGI!

\n" + yield "

Do note how the single-process nature of ashd-wsgi " + yield "allows data to be kept in memory persistently across requests, and " + yield "how the following counter increases by one for each request: " + str(n) + "

\n" + n += 1 + yield "

This script is a very raw WSGI example, using no glue code " + yield "whatever, but you should be able to use whatever WSGI middleware " + yield "you like in order to make it easier to code WSGI. If you have no " + yield "particular preference, I might recommend " + yield "WRW, " + yield "my WSGI Request Wrapper library, which is particularly made for " + yield "ashd-wsgi and therefore, for instance, allows " + yield "non-pickleble objects (like sockets) to be stored in sessions.

" + yield "

If you have installed the Python3 utilities as well, there is " + yield "also a Python3 script to demonstrate that " + yield "Python3 is supported as well.\n" + yield "

The current Python interpreter is " + ashd.wsgiutil.htmlquote(sys.version) + ".

" + yield "\n" + yield "\n" diff --git a/examples/python/wsgidir/py3.wsgi3 b/examples/python/wsgidir/py3.wsgi3 new file mode 100644 index 0000000..0dc7818 --- /dev/null +++ b/examples/python/wsgidir/py3.wsgi3 @@ -0,0 +1,14 @@ +import sys, ashd.wsgiutil + +def application(env, startreq): + startreq("200 OK", [("Content-Type", "text/html")]) + # Note that WSGI 3 requires returned data to be bytes, not strings. + yield b"\n" + yield b"Hello world from Python3!\n" + yield b"\n" + yield b"

Hello world from Python3!

\n" + yield b"

This example does nothing in particular except demonstrating " + yield b"that there is support for Python3 as well.

\n" + yield b"

The current Python interpreter is " + ashd.wsgiutil.htmlquote(sys.version).encode("utf-8") + b".

" + yield b"\n" + yield b"\n" diff --git a/examples/python/wsgidir/run b/examples/python/wsgidir/run new file mode 100755 index 0000000..5dbe5d2 --- /dev/null +++ b/examples/python/wsgidir/run @@ -0,0 +1,15 @@ +#!/bin/sh + +# Change to the directory containing this script +set -e +cd "$(dirname "$0")" + +# Invoke dirplex running in this directory, loading the wsgidir.rc +# configuration file. The same configuration can be put in +# e.g. /etc/ashd/dirplex.d or in any .htrc file. + +# The setsid command ensures that SIGINT is only received by htparser +# and not by dirplex or its children; it is not of any importance, but +# makes shutdown slightly cleaner, and hides the KeyboardInterrupt +# otherwise raised by Python. +htparser plain:port=8080 -- setsid dirplex -c ./wsgidir.rc . diff --git a/examples/python/wsgidir/wsgidir.rc b/examples/python/wsgidir/wsgidir.rc new file mode 100644 index 0000000..dd40a8f --- /dev/null +++ b/examples/python/wsgidir/wsgidir.rc @@ -0,0 +1,20 @@ +# Python 2 handler process +child wsgidir + exec ashd-wsgi ashd.wsgidir +# Python 3 scripts can also be served, using ashd-wsgi3 +child wsgidir3 + exec ashd-wsgi3 ashd.wsgidir + +# Dispatch any *.wsgi files to wsgidir +# See the Python documention for the ashd.wsgidir module for the +# meaning of the "xset python-handler chain" directive and what other +# values it can take. +match + filename *.wsgi + xset python-handler chain + handler wsgidir +# Do the same for Python3 scripts names *.wsgi3 +match + filename *.wsgi3 + xset python-handler chain + handler wsgidir3