[mod_python] Newbie: mod_python.publisher application structure

fabiovenni at tin.it fabiovenni at tin.it
Wed Jul 13 06:45:27 EDT 2005

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:

class Blog:

  def index(self, req):
    return """
      <form action="post" method="POST">
  Title: <input style="vertical-align: top;" type="text" name="title">
   Body: <textarea style="vertical-align: top;" name="body" rows=4 cols=20></textarea>
         <input type="submit">

  def post(self, req, title, body):
    return """
  Title: %s
   Body: %s

  <a href="save?title=%s&body=%s" title="save post">Save!</a>
  """ % (title, body, title, body)

  def save(self, req, title, body):
    import sys
    import time
    now = time.time()
    filename = req.document_root()+"/posts/"+str(now)[:-3]
    post = """<strong>%s</strong>\n%s""" % (title, body)
      fi = open(filename, 'w')
    except IOError:
      return '<pre>It hasn\'t been possible to save your post</pre>'

    return """
  <strong>%s saved!</strong>
  <a href=\"list\" title=\"List\">List</a>
    """ % (filename)

  def list(self, req):
    import os 
    from datetime import datetime
    dirname = str(req.document_root())+"/posts"
    output = "<html>\n<pre>\n"
    for post in os.listdir(dirname)[::-1]:
      output += "<em>"+str(datetime.fromtimestamp(float(post)))+"</em>\n"
      fi = open(dirname+"/"+post, 'r')
      output += fi.read()
      output += "<hr />\n"
    output += "\n\n<a href=\"/\" title=\"New Post\">New Post</a>\n</pre>\n</html>"
    return output

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?

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/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')

myblogdomain.net/post/timestamp         Resolved as blog.post('timestamp')

I've been playing around with this:
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

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?

I'm confused please help me


More information about the Mod_python mailing list