[Pythonmac-SIG] Shelve/pickle form=cgi.FieldStorage() data?

Bob Ippolito bob at redivi.com
Thu Sep 18 13:30:58 EDT 2003


On Thursday, Sep 18, 2003, at 13:12 America/New_York, Eric Wichterich 
wrote:

> I am trying to pickle data which are sent from an html form. The 
> result ( form=cgi.FieldStorage() ) is a list(?):
>
> FieldStorage(None, None, [MiniFieldStorage('picture_id', ' 13165 '), 
> MiniFieldStorage('picture_id', ' 13166 '), 
> MiniFieldStorage('ddloperation', 'edit'), 
> MiniFieldStorage('person_id', ' 3 ')])
>
> This means that a user with number 3 has chosen two pictures with 
> numbers 13165 and 13166 and wants to edit them. I just want to pickle 
> these values (=save the result as a file) so I can process them with 
> another python script  later.
> But always the error "can't pickle file objects" appears.

Well, this is the wrong list to ask such questions, because it has 
nothing to do with PythonMac specifically.. but in any case, you 
shouldn't be pickling the FieldStorage objects themselves, because they 
reference ephemeral things (for example, the file or file-like object 
associated with the request, FieldStorage.file).  There's also a good 
deal of information in that FieldStorage object you don't even want to 
preserve anyways (such as headers from the request).

You should transform your FieldStorage into a data structure like this:
{
	'picture_id': [13165, 13166],
	'ddloperation':['edit'],
	'person_id':[3],
}
which is certainly pickleable and will be far more efficient to work 
with.

Also, there are a lot of other web development frameworks for Python 
that are much higher level than the cgi module that you ought to look 
into.. Personally, I use Twisted for all of my networking code, but 
there are plenty of others that should make these tasks easier that may 
or may not require less of a paradigm shit than Twisted does.

-bob




More information about the Pythonmac-SIG mailing list