[mod_python] addhandler or sethandler

Jim Gallacher jpg at jgassociates.ca
Tue May 16 22:36:39 EDT 2006


Graham Dumpleton wrote:
> David Bear wrote ..
>> I'm trying to better understand the directive semantics. So, I created
>> 2 .htaccess files.
>>
>> the mpt.py file is the same. However I get different results between
>> each .htaccess file.
>>
>> .htaccess
>> SetHandler mod_python
>> PythonHandler mod_python.publisher
>> PythonDebug On
> 
> For SetHandler, any request against the directory will be processed
> by mod_python which in turn will bump request through to mod_python.publisher.
> 
>> ---
>> The above file produces an error when viewing mpt.py:
>>
>> Mod_python error: "PythonHandler mod_python.publisher"
>>
>> Traceback (most recent call last):
>>
>>   File "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line
>> 299, in HandlerDispatch
>>     result = object(req)
>>
>>   File "/usr/lib64/python2.4/site-packages/mod_python/publisher.py",
>> line 98, in handler
>>     path=[path])
>>
>>   File "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line
>> 457, in import_module
>>     module = imp.load_module(mname, f, p, d)
>>
>>   File "/work/servers/www/htdocs/apep/index.py", line 22
>>
>>     return "Falling through to main "
>>
>> SyntaxError: 'return' outside function
> 
> This looks like an indentation problem in your code file.
> 
>> However, the below htaccess file produces the expected result, it
>> executes the python function, which is:
>>
>> from mod_python import apache
>>
>> def handler ( req ):
>>   req.content_type = "text/plain"
>>   req.send_http_header()
>>   req.write( "mod python test \n hello world" )
>>   return apache.OK
>>
>>
>> .htaccess
>>  AddHandler python-program .py
>>  PythonHandler mpt
>>  PythonDebug On
> 
> Only requests for which URL resolves to physical file with a .py extension
> which be processed by mod_python which will in turn bump request
> through to "mpt" handler.
> 
> Note that I intentionally say extension of physical file URL mapped to
> as multiview matching rules can come into play. See link down further.
> 
> Note that "python-program" is same as saying "mod_python" in
> mod_python 3.X. Only "python-program" was accepted in mod_python 2.X.

Also note that in mod_python 3.x "req.send_http_header()" is not 
required, and is in fact a noop.

To get a better feeling for the difference between SetHandler and 
AddHandler I'd suggest using the same handler in each of your .htaccess 
files. Try accessing a couple of different files (.py, .txt, .html) 
using both setups and you'll get a pretty good idea of the difference 
between the 2 directives.

Jim


More information about the Mod_python mailing list