[mod_python] Apache/mod_python: Registering a request handler dynamically

Samuel Abels newsgroups at debain.org
Sun Dec 28 16:13:57 EST 2008


(I also sent this to comp.lang.python before realizing that this list is
the better place, so apologies if you got this message twice.)

Hi, 

Is there a way to dynamically overwrite the request handler from within
mod_python scripts? Something along those lines:

---------------
from mod_python import apache

def myhandler(request):
    request.content_type = 'text/plain'
    request.write('Hello world')

apache.set_default_handler(myhandler)
---------------

I specifically want to avoid changing the Apache directive, as this code
is supposed to function regardless of whether the user has access to
change the Apache directive.

The reason is that I am trying to hide the difference between different
environments (such as mod_python or CGI) from the developer, such that
the following is possible:

---------------
#!/usr/bin/python
import os, os.path
os.chdir(os.path.dirname(__file__))
from PleaseHideMyEnvironment import RequestHandler

def index(request):
    # Note that the request argument here is one created by
    # the PleaseHideMyEnvironment module, not a mod_python one.
    request.write('Hello World')

RequestHandler(index)
---------------

So at the time at which RequestHandler() is created, I need a way to
make sure that mod_python calls to the RequestHandler instead of the
normal handler, whenever a new request is made.

Any idea?

-Samuel 




More information about the Mod_python mailing list