[mod_python] Newbie: mod_python.publisher application structure

Jorey Bump list at joreybump.com
Wed Jul 13 16:25:15 EDT 2005

fabiovenni at tin.it wrote:
> I'm a totally newbie in python and mod_python. Just a couple of weeks of
> part-time toying.
> I've managed to write a minimal blog using the mod_python.publisher handler.
> This is the code, gross probably but does the job:
> Now, I was considering to build up a more complicated example and make it
> my blog actual engine. I've choosen to use publisher because I like the mapping
> URL --> callable object, but now I have some doubts that TFM doesn't solve,
> or probably it will in a couple of month time. What is a callable object
> in python?

Read the short manual from start to finish, it's very concise so it's 
easy to miss the finer points. Be sure to read the manual for your 
version of mod_python!

Here is a demo module to give some idea of what objects are published, 
although it's not exhaustive (and may even give slightly different 
results between mod_python versions). It will create links for you to 
try in your browser:


Demo module to show published objects when using mod_python.publisher.

import sys
from sys import path

_hidden = path
docstr = __doc__
mypassword = "BIGsecret!"

class SomeClass:
     "Uh-oh, it's OO"
     def __init__(self):
         self.someattr = "hello"
     def somefunction(self):
         return "hello"

# a published instance
someinstance = SomeClass()

def somefunction():
     "a published function"
     someattr = "hello"
     def somefunction():
         return "hello"
     return "hello"

dirlist = dir()

def index(req):
     "default function"
     header = "<html><head><title>Publisher demo</title></head><body><p>"
     footer = "</p></body></html>"
     link1 = '<a href="%s">http://host/handlerdir/demo/%s</a> <br>'
     link2 = '<a href="%s.%s">http://host/handlerdir/demo/%s.%s</a> <br>'
     link3 = '<a href="%s/%s">http://host/handlerdir/demo/%s/%s</a> <br>'
     links = ['<a href="../demo/">http://host/handlerdir/demo/</a> <br>']
     links.extend([link1 % (i, i) for i in dirlist])
     for o in ['someinstance', 'somefunction']:
         checklist = ['someattr', 'somefunction']
         links.extend([link2 % (o, i, o, i) for i in checklist])
         links.extend([link3 % (o, i, o, i) for i in checklist])
     return "\n".join([header, "\n".join(links), footer])

> As a web designer, navigation is very important to me so I plan websites
> starting from the URL, my problem is that I can't think a class structure
> that does the job:
> myblogdomain.net/ 
> myblogdomain.net/index     Resolved as the index page
> myblogdomain.net/archives/2005/jun/    Resolved as blog.archives('2005','Jun')
> myblogdomain.net/archives/2005           Resolved as blog.archives('2005')

You'll probably want to do this instead:


> I've been playing around with this:
> http://www.modpython.org/pipermail/mod_python/2005-March/017560.html
> but I'm confused on how to actually implement it. Do I have to put the Blog
> class inside a index.py file? and configure Apache like this:
>     <Directory /home/share/mod_python/first/>
>         SetHandler mod_python
>         PythonHandler mod_python.publisher
>         PythonDebug On
>     </Directory>
> What I'm searching for is an application structure suggestion more than a
> coding hint. I would like to have a single controller that handles requests
> and traslate them to Blog's method calls, probably proxied by a class that
> hanldes to fetch request parameters?

It can be tricky to run the entire site from index.py/index(). You may 
want to use SetHandler None in subdirectories that contain other 
resources, such as images, css, etc.

More information about the Mod_python mailing list