cgi python

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

    #1

    cgi python

    I going to use the cgi-handler (mod_python):




    If I test a simply py script it works

    code:
    ===========
    print "Content-type: text/html\n"
    print """
    <html>
    <body>
    <h1>TEST</h1>
    </body>
    </html>
    """
    ============

    But if I test a py script with cgi comments (import cgi):

    Part of the code
    ============
    import cgi

    #get HTTP query parameters
    query = cgi.FieldStorag e()

    #Get the selected year and month
    selectedYear = int(query["year"].value)
    selectedMonth = int(query["x"].value) + 1

    #Get the monthly revenue
    monthlyRevenue = float(query["value"].value)
    ============

    I get the following error:

    errormessage:
    =============== ===========
    Mod_python error: "PythonHand ler mod_python.cgih andler"

    Traceback (most recent call last):

    File "C:\Program
    Files\Python24\ Lib\site-packages\mod_py thon\apache.py" , line 299, in
    HandlerDispatch
    result = object(req)

    File "C:\Program
    Files\Python24\ Lib\site-packages\mod_py thon\cgihandler .py", line 96, in
    handler
    imp.load_module (module_name, fd, path, desc)

    File "C:/Program Files/Apache
    Group/Apache2/htdocs/clue/modules/templates\click pie.py", line 9, in ?
    selectedYear = int(query["year"].value)

    File "C:\Program Files\Python24\ Lib\cgi.py", line 559, in __getitem__
    raise KeyError, key

    KeyError: 'year'


    Who can help me?

  • Christian Hausknecht

    #2
    Re: cgi python

    Python_it wrote:
    [color=blue]
    > I going to use the cgi-handler (mod_python):
    >
    > http://www.modpython.org/live/mod_py.../hand-cgi.html
    >
    >
    > If I test a simply py script it works
    >
    > code:
    > ===========
    > print "Content-type: text/html\n"
    > print """
    > <html>
    > <body>
    > <h1>TEST</h1>
    > </body>
    > </html>
    > """
    > ============
    >
    > But if I test a py script with cgi comments (import cgi):
    >
    > Part of the code
    > ============
    > import cgi
    >
    > #get HTTP query parameters
    > query = cgi.FieldStorag e()
    >
    > #Get the selected year and month
    > selectedYear = int(query["year"].value)
    > selectedMonth = int(query["x"].value) + 1
    >
    > #Get the monthly revenue
    > monthlyRevenue = float(query["value"].value)
    > ============
    >
    > I get the following error:
    >
    > errormessage:
    > =============== ===========
    > Mod_python error: "PythonHand ler mod_python.cgih andler"
    >
    > Traceback (most recent call last):
    >
    > File "C:\Program
    > Files\Python24\ Lib\site-packages\mod_py thon\apache.py" , line 299, in
    > HandlerDispatch
    > result = object(req)
    >
    > File "C:\Program
    > Files\Python24\ Lib\site-packages\mod_py thon\cgihandler .py", line 96, in
    > handler
    > imp.load_module (module_name, fd, path, desc)
    >
    > File "C:/Program Files/Apache
    > Group/Apache2/htdocs/clue/modules/templates\click pie.py", line 9, in ?
    > selectedYear = int(query["year"].value)
    >
    > File "C:\Program Files\Python24\ Lib\cgi.py", line 559, in __getitem__
    > raise KeyError, key
    >
    > KeyError: 'year'
    >
    >
    > Who can help me?[/color]
    There is no Parameter 'year' in the Format-String sent by the Webserver!
    You can test it like this:
    if(query.has_ke y("yaer")):
    xyz = query["year"].value

    If you want to get a Parameter called 'year', you must call the cgi-Script
    like this:


    Another method is, to use a form in the calling html-Page and put a hidden
    value into it.

    Ciao,
    Christian

    Comment

    • Paul Boddie

      #3
      Re: cgi python

      Python_it wrote:[color=blue]
      > I going to use the cgi-handler (mod_python):
      >
      > http://www.modpython.org/live/mod_py.../hand-cgi.html
      >
      > If I test a simply py script it works[/color]

      You don't say how you test it, but I imagine that you just point your
      browser to the location where the program is published, without
      specifying any query parameters - see below for the significance of
      this.

      [...]
      [color=blue]
      > But if I test a py script with cgi comments (import cgi):
      >
      > Part of the code
      > ============
      > import cgi
      >
      > #get HTTP query parameters
      > query = cgi.FieldStorag e()
      >
      > #Get the selected year and month
      > selectedYear = int(query["year"].value)[/color]

      [...]
      [color=blue]
      > KeyError: 'year'[/color]

      The problem is that if you point your browser to this new program and
      don't specify query parameters (eg. ?year=2005&x=10 &value=5000 on the
      end of the URL) then attempting to get the "year" parameter from the
      query object will fail because the parameter doesn't exist - you didn't
      specify it.

      What you should do is to test for its presence first, just like you
      would with a normal Python dictionary, and I believe that the query
      object supports the has_key method:

      if query.has_key(" year"):
      selectedYear = int(query["year"].value)

      A more comprehensive alternative, given that the "year" parameter may
      not be a number, is to catch any exceptions raised when you try and get
      the parameter's value:

      try:
      selectedYear = int(query["year"].value)
      except KeyError:
      Do something here about the missing parameter.
      except ValueError:
      Do something here about a non-integer parameter.

      Testing Web applications can be hard, but the traceback tells you
      everything you need to know here.

      Paul

      Comment

      • Fredrik Lundh

        #4
        Re: cgi python

        Paul Boddie wrote:
        [color=blue]
        > Testing Web applications can be hard, but the traceback tells you
        > everything you need to know here.[/color]

        especially if you make sure to use the cgitb module:

        This module is no longer part of the Python standard library. It was removed in Python 3.13 after being deprecated in Python 3.11. The removal was decided in PEP 594. A fork of the module on PyPI c...


        while developing/debugging.

        </F>



        Comment

        Working...