CGI FieldStorage instances?

Tim Roberts timr at probo.com
Sun Nov 21 23:25:52 EST 2010


Gnarlodious <gnarlodious at gmail.com> wrote:
>
>I'm having a hard time understanding this, can someone explain?
>
>Running a CGI with query string:
>
>?action=Find&page=Data
>
>Script includes these lines:
>
>form=cgi.FieldStorage(keep_blank_values=1)
>print("Content-type:text/html\n\n")
>print(cgi.print_form(form))
>
>Output:
>
>Form Contents:
>
>action: <class 'cgi.MiniFieldStorage'>
>MiniFieldStorage('action', 'Find')
>page: <class 'cgi.MiniFieldStorage'>
>MiniFieldStorage('page', 'Data')
>
>It looks like every variable in the query string instantiates a
>MiniFieldStorage with that value, is that the case? 

Yes, unless it's a "file" type, then it is a full FieldStorage.

>And if so, what
>sort of cool tricks could I do with that feature? Because so far I am
>doing CGI and it is a big old mess. Intercepting every variable is
>complicated and confusing. Is there an easier way?

Have you looked at the source code?  That's the beauty of Python.  It's all
exposed for you.  MiniFieldStorage has a .name attribute and a .value
attribute.  So, for example:

    print form['action'].value
    print form['page'].value

If you're not sure whether the value will be specified:

    if 'action' in form:
        action = form['action'].value
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list