[mod_python] Flexible imports

Michael mogmios at mlug.missouri.edu
Sat Mar 6 19:52:22 EST 2004


Wouldn't you use .htaccess and Directory tags to set that up?

> Hi, all. I'm trying to do something with mod_python that is easy with
> CGIs, but is turning out to be somewhat challenging.
>
> I'd like to support python modules in a variety of subdirectories. So a
> url like
>
> http://localhost/top/something.py
>
> and
>
> http://localhost/top/next/somethingelse.py
>
> would both work. However, the way it seems to be set up, if you install
> a python handler (say in /top), then it gets executed for all .py files
> regardless of where they are (so in this case, somethingelse.py would be
> expected to be in /top, not /top/next). That can be useful at times, but
> at other times it can be a stumbling block.
>
> Here's a snippet of my handler:
>
> # --------------------
>
> def process_pyx(req):
> ~    path, filename = os.path.split(req.filename)
> ~    name, ext = string.split(filename, ".")
>
> ~    #pyx_mod = apache.import_module("%s/%s" % (path,name))
> ~    #pyx_mod = apache.import_module(name, path=path)
> ~    pyx_mod = apache.import_module("%s" % name)
>
> ~    pyx_mod.main(req)
> ~    return apache.OK
>
> def handler(req):
> ~    if args['requestedfile'].endswith('.py'):
>     return process_pyx(req)
> ~    elif args['requestedfile'].endswith('.xml'):
>     return process_xml(req)
> ~    else:
>     return apache.ERROR
>
> # --------------------
>
> This works fine for any .py file that is in the same directory as this
> handler. However, it doesn't work for subdirectories. You can see some
> attempts for adding a path for the import, but they don't work.
>
> So - is there an elegant way to allow a specifically named .py file to
> be executed from any directory under this handler's directory?




More information about the Mod_python mailing list