Strange behavior with os call in cgi script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sophie_newbie

    #1

    Strange behavior with os call in cgi script

    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.

  • Dennis Benzinger

    #2
    Re: Strange behavior with os call in cgi script

    sophie_newbie schrieb:[color=blue]
    > 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?
    > [...][/color]

    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.


    Comment

    • sophie_newbie

      #3
      Re: Strange behavior with os call in cgi script

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

      Comment

      • Rene Pijlman

        #4
        Re: Strange behavior with os call in cgi script

        sophie_newbie:[color=blue]
        >OK, interesting, but just how dow I print he environment in the
        >program??[/color]

        Add logging to your program:


        And log the environment:


        --
        René Pijlman

        Comment

        • Steve Holden

          #5
          Re: Strange behavior with os call in cgi script

          Rene Pijlman wrote:[color=blue]
          > sophie_newbie:
          >[color=green]
          >>OK, interesting, but just how dow I print he environment in the
          >>program??[/color]
          >
          >
          > Add logging to your program:
          > http://www.python.org/doc/2.3.5/lib/module-logging.html
          >[/color]
          Probably oiverkill, particularly for a beginner (is it only me that
          thinks the logging module is either way over-complicated or way
          under-documented?).
          [color=blue]
          > And log the environment:
          > http://www.python.org/dev/doc/newsty...-procinfo.html
          >[/color]

          regards
          Steve
          --
          Steve Holden +44 150 684 7255 +1 800 494 3119
          Holden Web LLC www.holdenweb.com
          PyCon TX 2006 www.python.org/pycon/

          Comment

          • Duncan Booth

            #6
            Re: Strange behavior with os call in cgi script

            Steve Holden wrote:
            [color=blue][color=green]
            >> Add logging to your program:
            >> http://www.python.org/doc/2.3.5/lib/module-logging.html
            >>[/color]
            > Probably oiverkill, particularly for a beginner (is it only me that
            > thinks the logging module is either way over-complicated or way
            > under-documented?).[/color]

            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).

            Comment

            • Rene Pijlman

              #7
              Re: Strange behavior with os call in cgi script

              Steve Holden:[color=blue]
              >Rene Pijlman:[color=green]
              >> Add logging to your program:
              >> http://www.python.org/doc/2.3.5/lib/module-logging.html
              >>[/color]
              >Probably oiverkill, particularly for a beginner (is it only me that
              >thinks the logging module is either way over-complicated or way
              >under-documented?).[/color]

              It struck me as somewhat complicated as well.

              Looking at the basic example:
              The official home of the Python Programming Language


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

              --
              René Pijlman

              Comment

              • Fredrik Lundh

                #8
                Re: Strange behavior with os call in cgi script

                Rene Pijlman wrote:
                [color=blue]
                > 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.[/color]

                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>



                Comment

                • Jim

                  #9
                  Re: Strange behavior with os call in cgi script

                  > But I do think that adding logging to a cgi script is a sensible thing to[color=blue]
                  > do for a beginner.[/color]
                  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

                  Comment

                  • Vinay Sajip

                    #10
                    Re: Strange behavior with os call in cgi script

                    Rene Pijlman wrote:[color=blue]
                    > 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.[/color]

                    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:



                    Regards,

                    Vinay Sajip

                    Comment

                    • Rene Pijlman

                      #11
                      Re: Strange behavior with os call in cgi script

                      Vinay Sajip:[color=blue]
                      >Rene Pijlman:[color=green]
                      >> It struck me as somewhat complicated as well.[/color][/color]
                      [color=blue]
                      >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[/color]

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

                      Add logging to your program:


                      And log the environment:


                      :-)

                      --
                      René Pijlman

                      Comment

                      Working...