cgi module question

Steve Holden sholden at holdenweb.com
Fri Jan 12 17:48:22 EST 2001


"Victor Muslin" <victor at prodigy.net> wrote in message
news:3a5f7b52.253219421 at localhost...
> Is there a way to parse a query string of this form:
>
> foo=bar&x=y=z&xyz
>
> Is this format against application/x-www-form-urlencoded guidelines?
> Can somebody point me to a definitive document?
>
> What I would ideally like is:
>
> { 'foo' : 'bar', 'x' : 'y=z', 'xyz' : None }
>
As in, for example, the cgi module docs:

parse_qs (qs[, keep_blank_values, strict_parsing])

Parse a query string given as a string argument (data of type
application/x-www-form-urlencoded). Data are returned as a dictionary. The
dictionary keys are the unique query variable names and the values are lists
of values for each name.

The optional argument keep_blank_values is a flag indicating whether blank
values in URL encoded queries should be treated as blank strings. A true
value indicates that blanks should be retained as blank strings. The default
false value indicates that blank values are to be ignored and treated as if
they were not included.

The optional argument strict_parsing is a flag indicating what to do with
parsing errors. If false (the default), errors are silently ignored. If
true, errors raise a ValueError exception.

It even works, see:

>>> d = cgi.parse_qs("foo=bar&x=y=z&xyz")
>>> d
{'foo': ['bar'], 'x': ['y=z']}

I think your problem is the missing "=" on xyz: see what happens if we keep
blank values and put "=" at the end of the query string.

>>> d = cgi.parse_qs("foo=bar&x=y=z&xyz=", 1)
>>> d
{'foo': ['bar'], 'xyz': [''], 'x': ['y=z']}

With strict parsing requested, an error is raised when the "=" is missing,
which presumably means this is not confirmant witht he required format:

>>> d = cgi.parse_qs("foo=bar&x=y=z&xyz", 1, 1)
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
  File "d:\python20\lib\cgi.py", line 169, in parse_qs
    for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
  File "d:\python20\lib\cgi.py", line 201, in parse_qsl
    raise ValueError, "bad query field: %s" % `name_value`
ValueError: bad query field: 'xyz'

However, I seem to remember PEP 222

(http://python.sourceforge.net/peps/pep-0222.html)

proposed a change to this. However, the PEP isn't likely to make it into
2.1 - everyone who might have commented is too busy putting web applications
together, or too lethargic to comment on it:

    cgi.py: Currently, query data with no `=' are ignored.  Even if
    keep_blank_values is set, queries like `...?value=&...' are
    returned with blank values but queries like `...?value&...' are
    completely lost.  It would be great if such data were made
    available through the FieldStorage interface, either as entries
    with None as values, or in a separate list.

Hope this helps.

it's-all-there-if-you-know-where-to-look-for-it-ly y'rs  - steve







More information about the Python-list mailing list