[mod_python] using callable classes instead of plain functions in handlers

Daniel Popowich dpopowich at comcast.net
Sun Oct 3 22:25:37 EDT 2004

> I'm in the process of working with mod_servlet after working with
> mod_publisher for a while.  I like it but I'm still working out for
> myself the discernable benefits it has over publisher. Anyhow...
> ...I tried recreating the above page login/authentication, however, it
> only worked when I overrode write_body_parts instead of write_html.
> Overriding write_html produced nothing.  The browser page just stayed on
> the page it was on before selecting the protected linked page.  I'm
> still trying to understand why...any pointers?

My fault.  Bad cut&paste.  The write_html method should have ended

    return True

Otherwise, by not returning anything it implicitly returns None, which
evaluates to false.  Bad.  Why?  You need to understand the respond
method which calls write_html.  Go here for an explanation:
http://lnx1.blue-fox.com/~dpopowich/mpstutorial/lifecycle and the
tutorial immediately following.

Also, to better explain "Authentication By Subclassing" than I have on
this list, I have put together a live demo, a picture being worth a
thousand words and all that.  Goto:


> Currently I use HTMLPage to form my page. But I use precompiled Cheetah
> templates for the body of each page. e.g. my index page looks like
> this...
> As an aside as I am using cheetah this way I take it I will have lots of
> small files like this if I have lots of pages, whereas in publisher I
> could have one index module holding many different page rendering
> functions. Is this the way it has to be with the class/servlet based
> approach?

I just built a very comprehensive content management system with
mpservlets that does what you want.  ONE servlet manages the whole
site.  It does this via PATH_INFO.  If you're not familiar with it you
should find out about it because it can be a very powerful tool.

If you have a URI, /some/dir/target, and a request comes in for
/some/dir/target/x/y/z then /some/dir/target will get the request with
PATH_INFO set to x/y/z.  Think of it as extraneous path information
that was passed along with the request.  Many systems use this to
create "clean" urls.

Now, if you have a servlet, /some/dir/myservlet, and a request comes
in for /some/dir/myservlet/this/file, then myservlet will have an
instance variable, path_info, set to ['this', 'file'].  (Aside: if a
request has no PATH_INFO then the path_info instance variable is set
to an empty list.)  Using this mechanism you can manage a site with a
few servlets using path_info as a lookup into a database or a


Daniel Popowich

More information about the Mod_python mailing list