[mod_python] sys.argv does not work when accessed with mod_python

Graham Dumpleton grahamd at dscpl.com.au
Wed Sep 14 22:17:17 EDT 2005


On 10/09/2005, at 10:53 PM, Sigurdur Einarsson wrote:

> Hello list,
>
> It seems like I'm unable to use sys.argv[0] with mod_python, it
> functions in the shell.  Is there something in mod_python that will do
> the same thing or is there a way to allow mod_python to access the
> function, assuming this is a security issue?

What exactly are you expecting to be obtaining from sys.argv[0]?

In a Python script run from the command line sys.argv[0] will yield the
pathname of the actual script which was run. Under mod_python, even if
sys.argv were able to somehow be set properly, the process corresponds
to an invocation of Apache "httpd" application.

Not knowing exactly what you are after, I'll give a few suggestions as
to what is available.

1. In any Python module file, you can access the global "__file__". This
will give the pathname to the file corresponding to that module. The
last component of the pathname may be useful in identifying the
resource. Ie., 'os.path.split(__file__)[1]'.

2. For a particular request, you can access "req.filename". This is the
physical resource as identified by Apache as being the target of a
request. Again, the last component of the pathname maybe useful.

3. Especially if you are using mod_python.publisher, req.path_info may
be useful. The value of this is what is used to identify the actual
function which is being executed.

Is what you are after the resource name as identified by a URI, or the
name of the particular function which was mapped to and called by
mod_python or some extension for mod_python, such as will be the case
with mod_python.publisher?

Graham



More information about the Mod_python mailing list