[mod_python] Flexible imports

Colin Fox cfox at cfconsulting.ca
Sat Mar 6 22:12:55 EST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael wrote:
| Wouldn't you use .htaccess and Directory tags to set that up?
|

I don't want to have tons of .htaccess files all over the place. I just
want to be able to make a directory, put a python file into that
directory and get it executed. The same way as would happen
automatically if I was using CGIs.

|> 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?
|
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFASr1noaQ1/feGlJoRAtxdAJ95DsLIev9oKIwDdD2zlDQW5dFYUwCeKGy2
GY42ZfcSiBhSLCHCiMpG2GE=
=XVux
-----END PGP SIGNATURE-----


More information about the Mod_python mailing list