questions concerning cgi.FieldStorage(keep_blank_values=1)

J

Jonas Meurer

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
 
D

Daniel Lichtenberger

Hi,

Jonas said:
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
 
J

Jonas Meurer

"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
 
D

Daniel Lichtenberger

Jonas said:
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
 
M

Michele Simionato

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top