mod python confusion

Robert Brewer fumanchu at amor.org
Sat May 29 11:14:08 EDT 2004


Kylotan wrote:
> > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/225299
> 
> Hi Robert. You've recommended this recipe to me in the past 
> when I asked a similar question on this newsgroup. However, 
> I must admit that I really don't understand 90% of it, 
> since there are so many layers of abstraction and it spans 
> several classes - and indeed, 2 recipes!
> Surely there has to be something simpler?

Okay, let me break down the steps for just mod_python, so you can follow
the logic. There is quite a bit of abstraction in the recipe, but for
the most part, it's very linear--just broken up into parts you can swap
out.

Let's put it all in one module, which you place in your apache
/htdocs/myapp folder. Say you have three web pages you wish to create: a
default page, a "view" page, and an "edit" page. They might look like
this:

def default(req):
    req.write("Nothing to see here")
    return apache.OK

def view(req):
    newData = util.FieldStorage(req, 1).list
    req.write("Now this is something")
    req.write(repr(newData))
    req.write("<br /><a href='edit.htm'>click me</a>")
    return apache.OK

def edit(req):
    req.write("Edited successfully.")
    return view(req)

In the same file, you can include the mod_python interface and
dispatching directly, if you want. The first part, the session
initialization, boils down procedurally to this:

from mod_python import apache, util

def dispatch(req):
    atoms = req.uri.split("/")
    script = atoms.pop()

    # At this point, "script" = the name of the page
    # which was requested. So we dispatch based on
    # the value of "script".

    dispatches = {'default.htm': default,
                  'view.htm': view,
                  'edit.htm': edit,
                  }
    
    try:
        nextHandler = self.dispatches[script]
    except KeyError:
        nextHandler = self.dispatches['default.htm']
    return nextHandler(req)


That's it for our demo module. The only thing left is to tell Apache
that all requests should be handled by our dispatch() function.
Therefore, in either the main Apache .conf file, or using another,
included .conf file (which is more work but more maintainable) we write:

<Directory /var/apache/htdocs/myapp>
  AddHandler python-program .htm
  PythonHandler dispatch
</Directory>

...and you now have a working mod_python app.

> Everything seems to suggest a major rewrite because I not only
> need to shift from printing everything to calling req.write or
> whatever, but I may also need to sort out some sort of dispatching
> mechanism mapping URLs to functions if I use mod_python directly.

Yes, you have a major rewrite on your hands. The reason I recommend
abstracting the webserver as in the recipe is to avoid having to rewrite
your application *again* the *next* time, when you stumble across Roxen
and decide that's the coolest webserver yet. :) If you want to talk
further about how to "meet in the middle", making an abstraction layer
that better fits your application style, let's talk! I certainly don't
use the recipe as-is; there's plenty to tweak.

I'll also quickly note one other thing abstraction buys you: you don't
have all of your core business logic hanging out in your /htdocs
folder--you can move that back into site-packages where it belongs. In
the recipe, the dispatch module (the only file in /htdocs) is only three
lines.


Hope that helps,

Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list