questions concerning cgi.FieldStorage(keep_blank_values=1)

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

    questions concerning cgi.FieldStorage(keep_blank_values=1)

    hello,

    i'm quite new to python. currently i try to write a web application with
    python cgi scripts.

    in this application, i need keys to be delivered with the url, some with
    and some without value (for example 'script.py?key1 &key2=foo'.

    i've searched the internet, and already figured out that i need to give
    non-empty keep_blank_valu es as argument to cgi.FieldStorag e, to make it
    not cut all the empty keywords.

    anyway, this still doesn't work really good:

    ---snip---
    form = cgi.FieldStorag e(keep_blank_va lues=1)

    print 'list keys with form.keys():'
    keys = form.keys()
    keys.sort()
    for key in keys:
    print key
    ---snip---

    if i request the script with script.py?key1& key2=foo, it will output:
    list keys with form.keys():
    key2

    any suggestions about how to make form.keys() contain the blank keys as
    well?

    bye
    jonas
  • Daniel Lichtenberger

    #2
    Re: questions concerning cgi.FieldStorag e(keep_blank_va lues=1)

    Hi,

    Jonas Meurer wrote:
    [color=blue]
    > if i request the script with script.py?key1& key2=foo, it will output:
    > list keys with form.keys():
    > key2
    >
    > any suggestions about how to make form.keys() contain the blank keys
    > as well?[/color]

    "key1" isn't a valid parameter, to supply an empty key you would write
    script.py?key1= &key2=foo

    Then cgi.FieldStorag e also includes key1.

    bye,
    Daniel

    --
    For mail replies please use my address from


    Comment

    • Jonas Meurer

      #3
      Re: questions concerning cgi.FieldStorag e(keep_blank_va lues=1)

      On 20/02/2005 Daniel Lichtenberger wrote:[color=blue][color=green]
      > > any suggestions about how to make form.keys() contain the blank keys
      > > as well?[/color]
      >
      > "key1" isn't a valid parameter, to supply an empty key you would write
      > script.py?key1= &key2=foo
      >
      > Then cgi.FieldStorag e also includes key1.[/color]

      great, it works. but is there no way to use single keywords as GET
      argument?

      bye
      jonas

      Comment

      • Daniel Lichtenberger

        #4
        Re: questions concerning cgi.FieldStorag e(keep_blank_va lues=1)

        Jonas Meurer wrote:[color=blue][color=green]
        >>"key1" isn't a valid parameter, to supply an empty key you would write
        >>script.py?key 1=&key2=foo
        >>
        >>Then cgi.FieldStorag e also includes key1.[/color]
        >
        > great, it works. but is there no way to use single keywords as GET
        > argument?[/color]

        You could manually parse the request string (CGI stores the request
        string as an environment variable, you can retrieve it via
        os.environ["REQUEST_STRING "]), but why not add "=1" (or "=") to your
        keywords?

        bye,
        Daniel

        --

        [visit for regular email address]

        Comment

        • Michele Simionato

          #5
          Re: questions concerning cgi.FieldStorag e(keep_blank_va lues=1)

          Jonas:[color=blue]
          > in this application, i need keys to be delivered with the url, some[/color]
          with[color=blue]
          > and some without value (for example 'script.py?key1 &key2=foo'.[/color]

          You are missing an "=" sign after key1. Confront with this example:

          from cgi import parse_qsl

          QS = "x=1&y=2&x=3&z= &y=4"
          print parse_qsl(QS)
          print parse_qsl(QS, keep_blank_valu es=True)

          which gives

          [('x', '1'), ('y', '2'), ('x', '3'), ('y', '4')]
          [('x', '1'), ('y', '2'), ('x', '3'), ('z', ''), ('y', '4')]

          Here the blank value "z=" is converted into "z=''".


          Michele Simionato

          Comment

          Working...