[mod_python] AddHandler / SetHandler (black magic)

Graham Dumpleton grahamd at dscpl.com.au
Fri Oct 6 07:01:41 EDT 2006


On 06/10/2006, at 7:40 PM, Norman Tindall wrote:

> Hello marinus,
>       You could try Set handler with
>       this code in handler:
>
> def handler(req,db):
>
>     if req.status != 200:
>         raise apache.SERVER_RETURN, req.status

Why??? In what circumstances do you believe that one could
arrive here with req.status already set to something other than
a value of 200. If an earlier handler had generated an error by
setting req.status, it should have returned apache.DONE and
not apache.OK and thus shouldn't ever end up here.

>     extentions_we_handle = ["html","py"]  # list of ext you want to  
> handle
>     ext = req.filename.split(".")

If you are going to do that, you should use:

   import posixpath
   ext = posixpath.splitext(req.filename)[0]

The way you were doing it, a '.' in a directory name earlier in the
URL could give problems.

The posixpath module is preferred instead of os.path as Apache
always uses POSIX style slash and not backslash for directory
separators. But then, posixpath doesn't deal with drive letters.
In practice there needs to be some apache.* functions which are
equivalent to os.path.* functions but which work properly on the
Apache style paths. Such a thing is on the list of things to do. :-)

>     if len(ext) > 1 and ext[-1] in extentions_we_handle:
>         return apache.DECLINED
>
>     # do some work here with the extantions you want to handle

Using AddHandler and other Apache directives is still the better
way. In mod_python 3.3 it will be easier to do things like above,
but it shouldn't be done in the response handler phase but an
earlier phase such as the fixup handler phase. I'll be writing an
article about this specific ability of mod_python 3.3 some time in
the near future hopefully.

Graham


More information about the Mod_python mailing list