mod_python Handler for Mako

One of my personal goals this Winter has been to learn and start using Python. Learning new languages and putting them to use is something I try to do about every year. Like so many others out there, messing around with Perl in college really has had an impact on my professional career. Being a professional web developer who uses Perl—especially through Apache/mod_perl—it was only natural that I began looking at building out a small site using mod_python. Little did I understand how much of a minefield I was stepping into!

Whenever I’m learning a new language, I inevitably explore how others are using it. This means trying out many of the Python-based web development frameworks that are out there. One of the biggest surprises for me was the reliance that nearly all of the frameworks place on custom Python application servers, bypassing Apache. This also extends to most of the template libraries. It all really depends on a pure Python application server. That would be fine if my goal was to learn Django or Zope. I’m interested in learning Python…and learning a new language for me has always been very dependent on reinventing a known wheel. For me, that really meant focusing on mod_python.

So I dive in, get mod_python installed on a system of mine and start working. Time from apt-get install mod_python to a working Hello World handler: about 15 minutes. All confident in my ability to get things moving, I start taking another look at Python template libraries. Everything grinds to a halt again for me almost immediately. The libraries all have documentation on how to call them from within python programs, but very little information on how to call them from within mod_python. Two exceptions stood out: Cheetah and Mako. Cheetah had a couple of handler examples on their wiki…but I was never able to get any of them to work well. Mako had a working WSGI handler that I was able to study and learn very quickly how to use their library from within mod_python.

Now, everything is up and running, and I’m building a couple of small web applications using mod_python and Mako. To help fill some of the vacuum out there with respect to mod_python handlers, I decided that I wanted to share the handler I have been working with. I wouldn’t call it production-worthy, but I am enjoying using it a lot. The Apache configurations required are below and I have attached the handler Python script. Enjoy!

Apache Configuration:

<Files ~ "\.html$">
    PythonPath "sys.path+['/var/www/www.braindonor.net/lib']"
    PythonDebug On
    SetHandler mod_python
    PythonHandler MakoHandler
</Files>
Attachments:
January 31st, 2009