questions concerning cgi.FieldStorage(keep_blank_values=1)

Discussion in 'Python' started by Jonas Meurer, Feb 20, 2005.

  1. Jonas Meurer

    Jonas Meurer Guest

    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_values as argument to cgi.FieldStorage, to make it
    not cut all the empty keywords.

    anyway, this still doesn't work really good:

    ---snip---
    form = cgi.FieldStorage(keep_blank_values=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
    Jonas Meurer, Feb 20, 2005
    #1
    1. Advertising

  2. Hi,

    Jonas Meurer wrote:

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


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

    Then cgi.FieldStorage also includes key1.

    bye,
    Daniel

    --
    For mail replies please use my address from
    http://perplex.schmumpf.de/
    http://www.gametective.de/
    Daniel Lichtenberger, Feb 20, 2005
    #2
    1. Advertising

  3. Jonas Meurer

    Jonas Meurer Guest

    On 20/02/2005 Daniel Lichtenberger wrote:
    > > any suggestions about how to make form.keys() contain the blank keys
    > > as well?

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


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

    bye
    jonas
    Jonas Meurer, Feb 20, 2005
    #3
  4. Jonas Meurer wrote:
    >>"key1" isn't a valid parameter, to supply an empty key you would write
    >>script.py?key1=&key2=foo
    >>
    >>Then cgi.FieldStorage also includes key1.

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


    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

    --
    http://perplex.schmumpf.de/
    [visit for regular email address]
    Daniel Lichtenberger, Feb 20, 2005
    #4
  5. Jonas:
    > 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'.


    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_values=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
    Michele Simionato, Feb 21, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al Stoltz
    Replies:
    1
    Views:
    1,000
    Andrew Clover
    Jan 23, 2004
  2. Fred Murray

    Odd cgi.FieldStorage behavior

    Fred Murray, Mar 2, 2004, in forum: Python
    Replies:
    1
    Views:
    305
    Tim Roberts
    Mar 4, 2004
  3. python user

    libapreq2 and cgi.FieldStorage

    python user, Jul 9, 2004, in forum: Python
    Replies:
    0
    Views:
    335
    python user
    Jul 9, 2004
  4. Austyn Bontrager

    cgi.FieldStorage and REDIRECT_REQUEST_METHOD

    Austyn Bontrager, Jul 28, 2004, in forum: Python
    Replies:
    1
    Views:
    468
    Lee Harr
    Jul 28, 2004
  5. Ames Andreas (MPA/DF)
    Replies:
    0
    Views:
    272
    Ames Andreas (MPA/DF)
    Aug 5, 2004
Loading...

Share This Page