[mod_python] Good example site with py, psp, and structure

Graham Dumpleton grahamd at dscpl.com.au
Mon Sep 25 07:41:18 EDT 2006


On 22/09/2006, at 6:22 PM, durumdara wrote:

> Hi !
>
> Eric Brunson írta:
>>> If I understand it good, I can specify one handler script for one  
>>> directory.
>>> Anywhere, where I specify a handler, the handler script working  
>>> for all .py (both for existing and not existing).
>>
>> Not precisely (and someone please help out if I express this  
>> incorrectly).  You can add a handler for dealing with .py files in  
>> which case .py files will be handled by that handler, or you can  
>> set a handler for URLs, which will perform a sort of module  
>> resolution algorithm on the URL.  In the first case, which I'm  
>> least familiar with, if you associate a handler with a .py file,  
>> referencing a file that doesn't exist will give you a 404 error.
>>
> When I used this model (see below),  I not got error: every py  
> filename
> accepted, but only mptest.py started:
>
>    LoadModule python_module modules/mod_python.so
>
>    <Directory c:/web/apache22/htdocs/>
>      AddHandler python-program .py
>      PythonHandler mptest
>      PythonDebug On
>    </Directory>
>
>    mptest.py:
>
>    from mod_python import apache
>
>    def handler(req):
>          req.content_type = 'text/html'
>          req.write('Handler: mptest.py<br>')
>          req.write('Actual (URL defined) filename: '+req.filename)
>          req.write('<br>Some random number: ')
>          import random
>          req.write(str(random.randint(0,1000)))
>          return apache.OK
>
> The URL I write:
>
>    http://localhost:7080/index_ssss.py
>
> The result is:
>
>    Handler: mptest.py
>    Actual (URL defined) filename: C:/web/apache22/htdocs/index_ssss.py
>    Some random number: 594
>
> The index_ssss does not exists, but the handler catched it, and handle
> the request with mptest.py.
>
> So I don't get 404 error, but this is good for me: I can handle every
> problem in one script !

The person who gave you the answer was using SetHandler and not
AddHandler, there is a significant difference. Most of his discussion
was also alluding to use of mod_python.publisher and not a custom
handler like you are using. The mod_python.publisher handler adds
an extra level of interpretation of the URL on top of basic mod_python
handler.

For difference between AddHandler and SetHandler in the context of
mod_python, see:

   http://www.dscpl.com.au/wiki/ModPython/Articles/ 
SetHandlerVersusAddHandler

This article may also go a long way to answering some of your other
questions in respect of how to avoid needing to specific a .py extension
all the time.

Graham




More information about the Mod_python mailing list