[mod_python] PythonOption and req.get_options()

Graham Dumpleton grahamd at dscpl.com.au
Mon Jun 13 18:26:40 EDT 2005

On 14/06/2005, at 3:12 AM, Joshua Ginsberg wrote:

> I must be missing something totally obvious here...
> I've got the base path and base URL for my application defined in  
> httpd.conf as a PythonOption directive:
>     PythonOption baseURL http://foo.bar.tld/path/to/app/
>     PythonOption basePath "/var/www/html/path/to/app/"
> And then in my PSP code, I'm opening a pickle file based on that  
> basePath directive:
> pyOptions = req.get_options()
> pckPath = pyOptions['basePath']+'signup_data.pck'
> ifs = open(pckPath)
> However, I get the following error:
>   File "/var/www/network/jag/signup/dnq.psp", line 9, in ?
>     ifs = open('signup_data.pck')
> IOError: [Errno 2] No such file or directory: 'signup_data.pck'

Don't put quotes around the value you set basePath to to as they will
not be removed, it will literally be set to what you put. Ie., use:

     PythonOption baseURL http://foo.bar.tld/path/to/app/
     PythonOption basePath /var/www/html/path/to/app/

Also recommend using:

   import os
   pyOptions = req.get_options()
   pckPath = os.path.join(pyOptions['basePath'],'signup_data.pck')

As you had your code originally, if you forget the trailing '/' on
the path it will fail. Using os.path.join() will ensure that a slash
is always added even if basePath doesn't include it.

BTW, there is nothing wrong with using hard coded paths in Apache
configuration files, but personally I don't like them as when you
move around directories of an application you potentially have to
change them.

As such, you might consider making the determination of the root
of your application more dynamic. You can do this if you always
have PythonHandler defined at the point of the top directory of
your web application. Ie., access "req.hlist" (mod_python 3.X)
and it will give you the name of the directory PythonHandler is
defined for.

   import os
   pckPath = os.path.join(req.hlist,'signup_data.pck')

Determining the base URL from req.hlist is possible but requires
more work. Look at the _ConfigCache._search() method of code in
the file:


for an example. In that case it is using the existence of a config
file as the root of where you are working with, stopping where the
PythonHandler directive is defined if no config file found. The result
of the search are variables defining handler root, config root, base
URL absolute and base URL relative etc. For example:


Note that base URL absolute doesn't include HTTP scheme or hostname
though as these can change dependent on many things. If these are
needed they should be determined from req, req.server and req.connection
as necessary.


More information about the Mod_python mailing list