[mod_python] Re: any way to get the current handler's directory?

Graham Dumpleton grahamd at dscpl.com.au
Sun May 1 02:06:01 EDT 2005


On 01/05/2005, at 3:40 PM, vegetax wrote:
>> Depends on which version of mod_python you are using. If you need to 
>> be
>> portable to older versions of mod_python, use:
>>
>>        if hasattr(req,"hlist"):
>>          # In mod_python 3.X have the req.hlist member.
>>          handler_root = req.hlist.directory
>>        elif hasattr(req,"get_dirs"):
>>          # In mod_python 2.X have the req.get_dirs() method.
>>          handler_root = req.get_dirs()["PythonHandler"]
>>
>> Graham
>
>
> Thanks graham, forgot to mention that i also need the uri part of the
> current handler,ie:
>
> <Directory "/var/www/mywebapp/handler_root">
>   SetHandler mod_python
>   PythonHandler myhandler
>   PythonOption dirpath "/mywebapp/handler_root"
> <Directory/>
>
> Any way to get this information in one step? without having to
> req.filename[len(req.hlist.directory): ]

Use a package which has worked it out for you and already provides a
dispatch mechanism. ;-)

That would at least save you having to work out how to do it yourself
which would I guess eliminate the step altogether. Have you ever looked
at Vampire (http://www.dscpl.com.au/projects/vampire).

Anyway, I would recommend not trying to cut corners, have seen it
many times on the list where people write their own dispatch mechanisms
not knowing that it will not work in all situations. Ie., would break
if done in mod_userdir directory, would break in a subdirectory of
handler root etc etc.

In Vampire where this sort of determination of additional path_info
is calculated, I use:

       handler_root = None

       if hasattr(req,"hlist"):
         # In mod_python 3.X have the req.hlist member.
         handler_root = req.hlist.directory
       elif hasattr(req,"get_dirs"):
         # In mod_python 2.X have the req.get_dirs() method.
         handler_root = req.get_dirs()["PythonHandler"]

       if handler_root is None:
         raise apache.SERVER_RETURN, apache.HTTP_INTERNAL_SERVER_ERROR

       length = len(req.filename) - len(handler_root)

       path_info = "/"

       if length != 0:
         path_info += req.filename[-length:]

What is it that you are actually trying to do? See if Vampire might
already provide what you need and save yourself some work. Am happy
to explain whether Vampire might help in what you are doing.

Graham



More information about the Mod_python mailing list