The modpy module builds upon the classes defined in the
cgi module to allow code originally
written with the CGI protocol in mind to be used unchanged with the
mod_python module.
See the documentation for the cgi
module for information on how to use the CGI abstraction layer. The only
difference with the modpy module is that instead of using
cgi.Request to call your handler
code, you use modpy.Request.
Example:
import jon.cgi as cgi
import jon.modpy as modpy
class Handler(cgi.Handler):
def process(self, req):
req.set_header("Content-Type", "text/plain")
req.write("Hello, world!\n")
def handler(modpy_req):
return modpy.Request(Handler).process(modpy_req)
wtWhen using modpy with the wt
templating system, the Apache setup is slightly different (although all your
templates and page code will be identical).
Most of the setup is the same as usual,
however the configuration for the /wt/ directory is different.
Instead of using a CGI script as the handler, a mod_python handler is used
instead:
SetHandler python-program
PythonHandler handler
You then place the code that you would usually put in
/cgi-bin/wt.py into /wt/handler.py. The global
function handler in this file will be called by mod_python to
handle requests, so you should also add this function, which should use
wt.Handler or your subclass of it
to handle the request:
def handler(modpy_req):
return modpy.Request(wt.Handler).process(modpy_req)
The base class for all exceptions defined by the modpy
module.
Request subclasses the
cgi.Request class and implements
the methods using mod_python's request class.
process(self, modpy_req)modpy_req: mod_python request instance
Returns: Apache reason code
Initialises the instance ready for a new request by calling the
_init method, then reads the
user input and sets up the various instance variables.
A cgi.Handler object of the type
passed to the
Request.__init__ method
is then instantiated and its
process method is called.
If an exception is thrown by this method then the
traceback method is called
to display it.
The value returned by this method is the value that the mod_python
handler function should return.
Note that there is a difference in how
modpy.Request and
cgi.CGIRequest handle the case
where the handler outputs a Location header without a hostname.
When using the CGI protocol, the Apache web server will issue an
internal redirect to handle the new URL. When using mod_python, this
is not possible and you should not output Location headers which
do not contain absolute URLs.
Example:
def handler(modpy_req):
return modpy.Request(wt.Handler).process(modpy_req)
For convenience, this class provides the standard
Request class with the
cgi.GZipMixIn already mixed
in.
Example:
def handler(modpy_req):
return modpy.GZipRequest(wt.Handler).process(modpy_req)
$Id: modpy.html,v c0f5f983a7fc 2010/03/23 01:07:17 jon $