Strange behavior with os call in cgi script

S

sophie_newbie

I have written a cgi script that seems to run perfectly from the
command line when I simulate some cgi input using
os.environ['QUERY_STRING'].

The thing is that when I run it from the browser, one of my os.system
calls, which gets excecuted fine when running the program in the
interpreter, doesn't seem to get excecuted, or gets excecuted but does
nothing.

Does anyone know whats going on or how I could debug this problem?

It is worth noting that other os.system calls in the script get
excecuted fine.
 
D

Dennis Benzinger

sophie_newbie said:
I have written a cgi script that seems to run perfectly from the
command line when I simulate some cgi input using
os.environ['QUERY_STRING'].

The thing is that when I run it from the browser, one of my os.system
calls, which gets excecuted fine when running the program in the
interpreter, doesn't seem to get excecuted, or gets excecuted but does
nothing.

Does anyone know whats going on or how I could debug this problem?
> [...]

Probably the environment is different when your program is executed by
your web server. So either PATH is wrong and your program is not found
or some other environment variable is wrong and you could debug it by
printing the environment in your program.
 
S

sophie_newbie

OK, interesting, but just how dow I print he environment in the
program??
 
D

Duncan Booth

Steve said:
Probably oiverkill, particularly for a beginner (is it only me that
thinks the logging module is either way over-complicated or way
under-documented?).

No, I would agree with you on that. I get the impression that it was
designed by studying some pre-existing logging packages and saying 'what
neat features can we take from each of these' rather than by writing up a
set of use-cases and saying 'what is the simplest way we can make all of
these possible'.

The way it is now means there is quite a step from the simplest possible
use to slightly more complex use. For example, the support for reading
configuration files is wonderfully general purpose, but far too much for
the vast majority of applications: you really don't want to expose a
typical end user to that sort of configuration soup. If all you want in
your application is to let the user specify the name of the logfile and the
logging level you are on your own (and you can't even convert the log level
name from a string to the required number without accessing an _ prefixed
variable in the logging module).
 
R

Rene Pijlman

Steve Holden:
Rene Pijlman:
Probably oiverkill, particularly for a beginner (is it only me that
thinks the logging module is either way over-complicated or way
under-documented?).

It struck me as somewhat complicated as well.

Looking at the basic example:
http://www.python.org/doc/2.3.5/lib/node304.html

.... the things that first-time users shouldn't be bothered with IMO are:

1. Getting a logger by name from a hierarchical namespace. There should be
a module-level log function that logs through the root logger.
2. Formatting the message (use a sensible default)
3. Adding a handler to a logger (artefact of the logging system).
4. Setting a log level (use a sensible default).

Perhaps there should be some module-level functions that make it easier to
'just log this message to that file'.

But I do think that adding logging to a cgi script is a sensible thing to
do for a beginner. Getting that to run in a debugger is probably way more
complicated.
 
F

Fredrik Lundh

Rene said:
But I do think that adding logging to a cgi script is a sensible thing to
do for a beginner. Getting that to run in a debugger is probably way more
complicated.

on the other hand, adding

if 1: # set to 0 when deploying
print "<pre>"
print cgi.escape(repr(os.environ))
print "</pre>"

to the CGI script isn't that hard... (if you're willing to do "view source" in
the browser, you can skip the <pre> and cgi.escape() stuff...)

</F>
 
J

Jim

But I do think that adding logging to a cgi script is a sensible thing to
do for a beginner.
Let me second that. I happen to write a lot of CGI, and ISTM that
while you can do it without logging, you are condemming yourself to a
lot of staring at the screen, head-scratching, and saying ``Now what
could *that* mean?'' That is, CGI programming without logging seems to
me to ba a lot like general programming in Perl. :-}

Jim
 
V

Vinay Sajip

Rene said:
It struck me as somewhat complicated as well.

Looking at the basic example:
http://www.python.org/doc/2.3.5/lib/node304.html

... the things that first-time users shouldn't be bothered with IMO are:

1. Getting a logger by name from a hierarchical namespace. There should be
a module-level log function that logs through the root logger.
2. Formatting the message (use a sensible default)
3. Adding a handler to a logger (artefact of the logging system).
4. Setting a log level (use a sensible default).

Perhaps there should be some module-level functions that make it easier to
'just log this message to that file'.

But I do think that adding logging to a cgi script is a sensible thing to
do for a beginner. Getting that to run in a debugger is probably way more
complicated.

You should look at later versions of Python - your points above about
easier configuration have already been addressed: here's a link from
the current (2.4) docs:

http://docs.python.org/lib/minimal-example.html

Regards,

Vinay Sajip
 
R

Rene Pijlman

Vinay Sajip:
Rene Pijlman:
You should look at later versions of Python - your points above about
easier configuration have already been addressed: here's a link from
the current (2.4) docs:

http://docs.python.org/lib/minimal-example.html

Yes, that looks good. Thanks for pointing that out. So... my advice to OP
(if still alive) is:

Add logging to your program:
http://docs.python.org/lib/minimal-example.html

And log the environment:
http://www.python.org/dev/doc/newstyle/lib/os-procinfo.html

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top