Andy's insights

Opinions, thoughts, archivements

Sat, 18 Mar 2006

Using PyBlosxom

Searching for an extendable but simple weblog system, I finally found PyBlosxom.

This one is really simple and easy to extend. As a prove of concept I wrote the mod_python hander to integrate the blog in Apache 2. There is an old code snippled for this purpose somewhere out there. But this one won't work with recend software.

Pyblosxom mod_python handler

Based on ideas from 
 * Wari Wahab <wari at>
 * pyblosxom.cgi 1.3.2

from mod_python import apache
import sys
from config import py as cfg

if cfg.has_key("codebase"):
    sys.path.insert(0, cfg["codebase"])

from Pyblosxom.pyblosxom import PyBlosxom

__author__ = 'Andreas Stricker <>'
__version__ = "1.3.2"
__copyright__ = "Copyright (c) 2006 Andreas Stricker"
__license__ = "Python"

def handler(req):
    req.content_type = 'text/html'

    env = {}
    for k, v in req.subprocess_env.items():
        env[k] = v

    env['wsgi.input'] = req
    env['wsgi.errors'] = sys.stderr

    # setup url_scheme for static rendering
    if cfg.has_key('base_url'):
    	if cfg['base_url'].find('://') > 0:
            env['wsgi.url_scheme'] = cfg['base_url'][:cfg['base_url'].find("://")]
            env['wsgi.url_scheme'] = "http"
	    if cfg['base_url'].startswith('/'):
	    	cfg['base_url'] = "http://" + req.hostname + cfg['base_url']
        env['wsgi.url_scheme'] = "http"
        cfg['base_url'] = "/blog/"

    p = PyBlosxom(cfg, env)

    response = p.getResponse()

    # detect if we have to return "404 Not found" status
    # This case is handled as suggested in mod_python documenation
    if response.status.startswith('404'):
        req.status = apache.HTTP_NOT_FOUND

    # set headers
    for k, v in response.headers.items():
        if k.lower() == 'content-type':
            req.content_type = v
        req.headers_out[k] = v


    return apache.OK

To make it work, you must register this script as handler for a specific directory:

AddHandler mod_python .py
PythonHandler blog
#PythonDebug On

posted at: 14:58 | path: /python | permanent link to this entry