Help setting CGI Environment Variables

Roman Suzi rnd at onego.ru
Sun May 27 04:34:25 EDT 2001


On Sat, 26 May 2001, Ari David Greenberg wrote:

>Thanks for your help. This approach DOES let my code run correctly, in
>that it displays TEST_VAR as an environment variable. However, this
>doesn't really set the environment variable for this process.
>
>According to the python documentation, "such changes to the environment
>affect subprocesses started with os.system(),popen() or fork() and
>execv().
>
>Is there any way to change the environment variables to affect the current
>process? If not, what would be the easiest way of starting a subprocess to
>take advantage of the new environment vars. Thanks

I think, Python uses os.environ for it's needs.
For example, one can set locale thru

os.environ["LC_ALL"] = "C"
before calling getdefaultlocale

etc

But when you look at

$ ps aex

you will not see any changes :-(

PLease, look also into the Python FAQ (Q 4.18) for
the explanation and code example for child processes.

*

The question remains open: how to change environment
of the main process of Python program?
For example, how to change process name from
./redirector.py ... -options-
to just
redirector

(this way others will not see options and it could be considered secure).

In Perl, IIRC, changing $argv[0] does the trick.

>-Ari
>
>
>On Sun, 27 May 2001, Roman Suzi wrote:
>
>> On Sat, 26 May 2001, Ari David Greenberg wrote:
>>
>> >I am trying to write a CGI script that must modify some CGI environment
>> >variables. How should I do this? I tried the following:
>> >
>> >#!/usr/pubsw/bin/python
>> >import sys, os
>> >os.putenv("TEST_VAR", "/abc")
>> >
>> >for name, value in os.environ.items():
>> >	print "%s\t= %s" % (name, value)
>>
>> #!/usr/bin/python
>> import sys, os
>> os.environ["TEST_VAR"] = "/abc"
>> for name, value in os.environ.items():
>>         print "%s\t= %s" % (name, value)
>>
>> gives you this result.
>>
>> *
>>
>> However, I'd liked to do it this way:
>>
>> exec """
>> TEST_VAR1 = "/abc"
>> TEST_VAR2 = "/abc"
>> """ in os.environ.data
>>
>> for name, value in os.environ.items():
>>         print "%s\t= %s" % (name, value)
>>
>>
>> - but it remains unclear if this is legal. (os.environ is
>> an object).
>>
>> >
>> >
>> >I ran this program from the command prompt, and it printed all my
>> >environment variables however it did not print TEST_VAR. I am concerned
>> >that I am not setting TEST_VAR properly. I thought os.putenv(<name>,
>> ><value>) was the way to do it, but I'm having difficulties.
>> >
>> >Any help greatly appreciated. Please email me.
>> >
>> >-Ari
>> >
>> >
>>
>> Sincerely yours, Roman Suzi
>> --
>> _/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
>> _/ Sunday, May 27, 2001 _/ Powered by Linux RedHat 6.2 _/
>> _/ "Hard work must have killed someone!" _/
>>
>>
>>
>
>

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Sunday, May 27, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Hard work must have killed someone!" _/





More information about the Python-list mailing list