CGI: Assign FieldStorage values to variables

Chris Rebert clp2 at rebertia.com
Wed Aug 17 12:23:01 EDT 2011


On Wed, Aug 17, 2011 at 2:19 AM, Gnarlodious <gnarlodious at gmail.com> wrote:
> I should add that this does what I want, but something a little more
> Pythonic?
>
> import cgi, os
> os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3"
> form=cgi.FieldStorage()
>
> form
>
> dict = {}
> for key in form.keys(): dict[ key ] = form[ key ].value
>
> dict
> locals().update(dict)
> name3

Try it within a function. It will fail epic-ly in CPython and most
other implementations.
Read the note in the locals() docs:
http://docs.python.org/library/functions.html#locals

Cheers,
Chris



More information about the Python-list mailing list