CGI and logging module

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

    CGI and logging module

    I am using:

    Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
    [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4

    with Apache/1.3.26. The following script causes an Internal Server Error
    (with nothing in the Apache error logs AFAIK):

    #!/usr/local/bin/python

    import logging

    print "Content-type: text/html\n\n"
    print "<html><head><t itle></title></head><body><h1> OK</h1>"""
    print "</body></html>"

    The same script with the import statement commented out works as expected.
    Has anyone encountered this? I have googled without success.

    Thanks

    Peter


  • Peter Hansen

    #2
    Re: CGI and logging module

    Peter Mott wrote:[color=blue]
    > I am using:
    >
    > Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
    > [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    >
    > with Apache/1.3.26. The following script causes an Internal Server Error
    > (with nothing in the Apache error logs AFAIK):
    >
    > #!/usr/local/bin/python
    >
    > import logging
    >
    > print "Content-type: text/html\n\n"
    > print "<html><head><t itle></title></head><body><h1> OK</h1>"""
    > print "</body></html>"
    >
    > The same script with the import statement commented out works as expected.
    > Has anyone encountered this? I have googled without success.[/color]

    Can't help on the specific problem, but two points:

    1. You are supposed to be returning \r\n, I believe, not just
    \n between lines.

    2. If you want to see what is going wrong, put a try/except
    around the import statement and try to write something useful
    along with your results:

    err = ''
    try:
    import logging
    except Exception, ex:
    err = str(ex)

    print 'Content-type: test/html\r\n\r\n'
    blah blah blah
    print '<p>Error: %s</p>\r\n'

    If you can manage to import the sys and traceback modules,
    you can get a more fully formatted exception using
    ''.join(traceba ck.format_excep tion(*sys.exc_i nfo()))

    -Peter

    Comment

    • Peter Otten

      #3
      Re: CGI and logging module

      Peter Hansen wrote:
      [color=blue]
      > 2. If you want to see what is going wrong, put a try/except
      > around the import statement and try to write something useful
      > along with your results:
      >
      > err = ''
      > try:
      > import logging
      > except Exception, ex:
      > err = str(ex)
      >
      > print 'Content-type: test/html\r\n\r\n'
      > blah blah blah
      > print '<p>Error: %s</p>\r\n'
      >
      > If you can manage to import the sys and traceback modules,
      > you can get a more fully formatted exception using
      > ''.join(traceba ck.format_excep tion(*sys.exc_i nfo()))[/color]

      I've just discovered the cgitb module, which offers a minimally intrusive
      way to debug cgi scripts - just add

      import cgitb
      cgitb.enable()

      at the top of the script.

      (Yet another)
      Peter

      Comment

      • Andrew James

        #4
        Re: CGI and logging module

        Peter,
        Have you considered using mod_python as an alternative to CGI? It offers
        a number of benefits including partial session support, integration into
        Apache's internals and caching bytecode. I can provide you with some
        sample code if you wish (although the online documentation is very good)

        Regards,
        Andrew

        On Thu, 2004-11-18 at 22:37 +0100, Peter Otten wrote:[color=blue]
        > Peter Hansen wrote:
        >[color=green]
        > > 2. If you want to see what is going wrong, put a try/except
        > > around the import statement and try to write something useful
        > > along with your results:
        > >
        > > err = ''
        > > try:
        > > import logging
        > > except Exception, ex:
        > > err = str(ex)
        > >
        > > print 'Content-type: test/html\r\n\r\n'
        > > blah blah blah
        > > print '<p>Error: %s</p>\r\n'
        > >
        > > If you can manage to import the sys and traceback modules,
        > > you can get a more fully formatted exception using
        > > ''.join(traceba ck.format_excep tion(*sys.exc_i nfo()))[/color]
        >
        > I've just discovered the cgitb module, which offers a minimally intrusive
        > way to debug cgi scripts - just add
        >
        > import cgitb
        > cgitb.enable()
        >
        > at the top of the script.
        >
        > (Yet another)
        > Peter
        >[/color]
        --
        Andrew James <drew@gremlinho sting.com>

        Comment

        • Peter Mott

          #5
          Re: CGI and logging module

          Thanks for these responses all useful. I had completed a fairly large script
          which threw these unrecorded errors at me. Took ages to locate the soiurce
          of the problem. I should have used the try: except: round the import
          statement (I was using the 'import cgitb;cgitb.ena ble()' mantra but _after_
          the impoprt statement :-( ).

          As I said I am using Python on FreeBSD:

          Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
          [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> import logging
          >>>[/color][/color][/color]

          and I imported loggng successfully. When I modify my script to the
          following:
          ===============
          #!/usr/local/bin/python

          try:
          import logging
          except Exception, e:
          print "Content-type: text/html\n"
          print "<html><head><t itle></title></head><body><p>" + str(e) +"<p>"""
          else:
          print "Content-type: text/html\n"
          print
          "<html><head><t itle></title></head><body><h1> OK</h1></body></html>"""

          ==============
          It reports the error which is " No module named logging " . Something is
          wrong here. Does anyone have any ideas?

          Peter



          "Peter Mott" <peter@monicol. co.uk> wrote in message
          news:419d0a70$0 $221$bed64819@n ews.gradwell.ne t...[color=blue]
          >I am using:
          >
          > Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
          > [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
          >
          > with Apache/1.3.26. The following script causes an Internal Server Error
          > (with nothing in the Apache error logs AFAIK):
          >
          > #!/usr/local/bin/python
          >
          > import logging
          >
          > print "Content-type: text/html\n\n"
          > print "<html><head><t itle></title></head><body><h1> OK</h1>"""
          > print "</body></html>"
          >
          > The same script with the import statement commented out works as expected.
          > Has anyone encountered this? I have googled without success.
          >
          > Thanks
          >
          > Peter
          >[/color]


          Comment

          • Peter Otten

            #6
            Re: CGI and logging module

            Andrew James wrote:
            [color=blue]
            > Peter,
            > Have you considered using mod_python as an alternative to CGI? It offers
            > a number of benefits including partial session support, integration into
            > Apache's internals and caching bytecode. I can provide you with some
            > sample code if you wish (although the online documentation is very good)[/color]

            As you may have noted, "Peter" is a bit ambiguous in this thread :-)
            If you meant to address me, I use CGIs only for personal purposes on the
            local machine - basically as a substitute for command line tools with fancy
            output. mod_python's CGI handler does not seem to offer any advantage here,
            or does it?

            Peter


            Comment

            • Peter Otten

              #7
              Re: CGI and logging module

              Peter Mott wrote:
              [color=blue]
              > As I said I am using Python on FreeBSD:
              >
              > Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
              > [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
              > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
              >>>> import logging
              >>>>[/color][/color]
              >
              > and I imported loggng successfully. When I modify my script to the
              > following:
              > ===============
              > #!/usr/local/bin/python
              >
              > try:
              > import logging
              > except Exception, e:
              > print "Content-type: text/html\n"
              > print "<html><head><t itle></title></head><body><p>" + str(e) +"<p>"""
              > else:
              > print "Content-type: text/html\n"
              > print
              > "<html><head><t itle></title></head><body><h1> OK</h1></body></html>"""
              >
              > ==============
              > It reports the error which is " No module named logging " . Something is
              > wrong here. Does anyone have any ideas?[/color]

              Is 2.3.4 the only python interpreter on your system? You could use
              sys.version to verify that you are not accidentally using an older (pre
              2.3) version that does not offer that package.

              Peter

              Comment

              • Michael Ströder

                #8
                Re: CGI and logging module

                Peter Otten wrote:[color=blue]
                >
                > import cgitb
                > cgitb.enable()[/color]

                But make sure to turn it off when going public with your service since
                it reveals interesting information to an attacker.

                Ciao, Michael.

                Comment

                • Peter Mott

                  #9
                  Re: CGI and logging module

                  This was a Columbus Egg. The version of Python Apache used had reverted to
                  2.2.2 after a recent system failure. The version used for console logins was
                  2.3.4. I'll contact my ISP :-). Thanks for all the help.

                  Peter

                  "Peter Mott" <peter@monicol. co.uk> wrote in message
                  news:419d0a70$0 $221$bed64819@n ews.gradwell.ne t...[color=blue]
                  >I am using:
                  >
                  > Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
                  > [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
                  >
                  > with Apache/1.3.26. The following script causes an Internal Server Error
                  > (with nothing in the Apache error logs AFAIK):
                  >
                  > #!/usr/local/bin/python
                  >
                  > import logging
                  >
                  > print "Content-type: text/html\n\n"
                  > print "<html><head><t itle></title></head><body><h1> OK</h1>"""
                  > print "</body></html>"
                  >
                  > The same script with the import statement commented out works as expected.
                  > Has anyone encountered this? I have googled without success.
                  >
                  > Thanks
                  >
                  > Peter
                  >[/color]


                  Comment

                  • Andrew Clover

                    #10
                    Re: CGI and logging module

                    Peter Mott <peter@monicol. co.uk> wrote:
                    [color=blue]
                    > The same script with the import statement commented out works as expected.
                    > Has anyone encountered this?[/color]

                    You'll need to find out what the error is to go further (see other
                    posts) but as a tip: problems like this are sometimes caused by
                    insufficient permissions.

                    If you have root, try 'su'-ing to the Apache user (eg. 'su nobody')
                    and running the script directly ('./script.py') - does it work?

                    --
                    Andrew Clover
                    mailto:and@doxd esk.com

                    Comment

                    • Peter Hansen

                      #11
                      Re: CGI and logging module

                      Peter Mott wrote:[color=blue]
                      > This was a Columbus Egg. The version of Python Apache used had reverted to
                      > 2.2.2 after a recent system failure. The version used for console logins was
                      > 2.3.4. I'll contact my ISP :-). Thanks for all the help.[/color]

                      Cool, glad to help. :-)

                      I was just going to point out that importing builtin modules such
                      as 'sys' should "always" work, so you could print the values of
                      sys.modules, sys.prefix, sys.executable, and so forth, to get
                      a fix on what was what. Looks like you didn't need that though.

                      -Peter

                      Comment

                      Working...