[mod_python] conditions for a 404

Jorey Bump list at joreybump.com
Thu May 25 14:55:25 EDT 2006

David Bear wrote:
> I have a file called tddb.py which contains
> """
> from mod_python import session
> def index(req):
>   req.content_type = "text/plain"
>   req.send_http_header()
>   req.write("hello world")

These statements are unnecessary when using Publisher.

>   keys = req.keys()

req has no keys method.

>   for k in keys:
>     req.write("object name is %s " % k)
>   req.write("done")

req.write is unnecessary with Publisher, although it may be useful in 
some cases.

>   return apache.OK

This is also unnecessary. It's usually best to return a string from your 
functions when using Publisher. In most cases, this will be a complete 
HTML page, but could also be plain text. In either case, Publisher will 
write the correct headers.

Here is a simplified version of your module:

def index(req):
     return "\n".join(["object name is %s " % (i,) for i in dir(req)])

> It lives in a directory where publisher is set as the handler for all
> .py files.
> When I run this via url http://myserver/tddb.py apache returns a 404
> error.

With Publisher, you must call a function within the module. These are 


And, because of the special way Publisher handles index():


Your URL should also have worked, but it is bad form and maybe you are 
not using a recent version of mod_python. That would be a mistake. You 
should upgrade.

> I've checked that the file perms on 755 and group own by the webserver
> group.
> I'm getting lost here. the more I use modpython the less I understand
> it. What could be wrong?

It is very different from other web application languages, so forget 
everything you know about PHP or CGI for now, as they will only create 
false expectations. Pretend that you are learning web programming from 
scratch, and you may eventually find that it makes a lot of sense. There 
are caveats, however, and mod_python itself is a bit of a moving target 
while it is evolving. Be sure to read the documentation aimed at your 
specific version, and always report your apache/mod_python versions when 
asking for help.

I recommend that beginners master Publisher and ignore PSP for the 
timebeing. That will help you get in the right frame of mind, and 
Publisher is simple (yet powerful) enough to study when/if you decide to 
write your own handler.

More information about the Mod_python mailing list