[mod_python] transhandler setup?

Graham Dumpleton grahamd at dscpl.com.au
Thu Aug 25 22:19:42 EDT 2005


If your content handler doesn't want to handle a request, have it return
apache.DECLINED and Apache will then serve up any static file that may
have been identified by the URL.

   def handler(req):
     extension = os.path.splitext(req.filename)[1]
     if extension == ".html":
       if not os.path.exists(req.filename):
         ...
       else:
         return apache.DECLINED
     else:
       return apache.DECLINED

That is at least one way of doing it, although this will disable
processing in some cases by other Apache modules such as PHP. You can
get around that by explicitly disabling use of mod_python for that
extension:

   <Directory /document/root>
   SetHandler mod_python
   PythonHandler main
   <Files *.php>
   SetHandler None
   </Files>
   </Directory>

Rather than using subtraction, you could instead be additive:

   <Directory /document/root>
   AddHandler mod_python .html
   PythonHandler main
   </Directory>

Thus, if you are only interested in intercepting .html files, only
have mod_python triggered for them.

In respect of transhandler(), all I can really comment on there is
that if setting PythonTransHandler, you do not need to use either
AddHandler or SetHandler. These latter directives only need to be
used if setting PythonHandler, ie., the content handler. As the
documentation points out, PythonTransHandler can only be used at
top level scope, ie., not with a Directory, Location or Files
directive or in a .htaccess file.

Graham

On 26/08/2005, at 11:57 AM, IR labs wrote:

> Sorry, I haven't been clear. I don't want so much to handle all 
> requests under a directory. I like to handle all requests in general, 
> and than first analyze the url. ((If the it is a certain file (.gif, 
> .jpg, .css, .js) or a certain directory, apache should handle it 
> further, i.e. just serve that file. If it is just a path or a .html, I 
> want a python script to check wether the directory or file indeed 
> exist or not. If the index.html or other .html file doesn't exist the 
> main.py should take it over.))
> So I wan't this analysis be done during the transHandler phase. But 
> how do I make apache run my translate.py first? where do I put my 
> config lines?
> PythonPath "sys.path + ['/some/path']"
> PythonTransHandler translate
>
> thx
> dirk
>
> On 25-aug-05, at 23:01, Graham Dumpleton wrote:
>
>> If you want all requests falling under a directory to be handled by
>> mod_python use SetHandler and not AddHandler. Eg.
>>
>>   <Directory /some/path>
>>   SetHandler mod_python
>>   PythonHandler main
>>   </Directory>
>>
>> Read the documentation for further information of the difference.
>>
>> On 25/08/2005, at 9:21 PM, IR labs wrote:
>>
>>> Hi All, a mod_python newbie here.
>>>
>>> I am trying to set up my server using mod_python's transhandler, but 
>>> I can't figure out the right configuration. I am experimenting with 
>>> the most crude and basic uri translation possible: every request 
>>> should be redirected to main.py. But somehow apache doesn't want to 
>>> do that. Right now it still displays static pages (.html files) with 
>>> images, etc etc. (where I would think req.filename = 
>>> '/var/www/mod_python_test/main.py' should prevent all of that.)
>>>
>>> This is my setup:
>>>
>>> from httpd.conf :
>>> -----------------------------------------
>>> ...
>>> <IfModule mod_python.c>
>>>     AddHandler mod_python .py
>>>     PythonPath "sys.path + ['/var/www/mod_python_test/']"
>>>     PythonTransHandler translate
>>>
>>> </IfModule>
>>> ...
>>> -----------------------------------------
>>>
>>> my script
>>> /var/www/mod_python_test/translate.py :
>>> -----------------------------------------
>>> from mod_python import apache
>>>
>>> def transhandler(req):
>>> 	req.filename = '/var/www/mod_python_test/main.py'
>>> 	return apache.OK
>>> -----------------------------------------
>>>
>>> using
>>> FreeBSD 5.2.1
>>> Apache 2.0.54
>>> mod_python 3.1.4
>>> Python 2.3.2
>>>
>>>
>>> Note that my main.py does work through the .htaccess in 
>>> /var/www/mod_python_test/
>>> -----------------------------------------
>>> AddHandler mod_python .py
>>> PythonHandler main
>>> PythonDebug On
>>> -----------------------------------------
>>>
>>> But it (main.py) should be called for all the url's requested (not 
>>> just for /mod_python_test/whatever.py and friends.)
>>> thx
>>> dirk
>>>
>>>
>>>
>>>
>>> -----------------------------
>>> Dirk van Oosterbosch
>>> de Wittenstraat 225
>>> 1052 AT Amsterdam
>>> the Netherlands
>>>
>>> http://labs.ixopusada.com
>>> -----------------------------
>>>
>>>
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
>>
>
> -----------------------------
> Dirk van Oosterbosch
> dirk at ixopusada.com
> -----------------------------
>



More information about the Mod_python mailing list