This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author quentel
Recipients amaury.forgeotdarc, barry, eric.araujo, flox, ggenellina, giampaolo.rodola, gvanrossum, l0nwlf, oopos, pebbe, quentel, r.david.murray, tcourbon, tercero12, tobias, v+python
Date 2011-01-02.08:51:37
SpamBayes Score 1.7328916e-12
Marked as misclassified No
Message-id <1293958302.14.0.785429236239.issue4953@psf.upfronthosting.co.za>
In-reply-to
Content
Hi,

I have started working on the port of a simplified version of Karrigell (a web framework) to Python3. I experienced the same problem as the other posters : in the current version, file upload doesn't work. So I've been working on the cgi module for a few days and now have a version which correctly manages file uploads in the tests I made

The problem in the current version (3.2b2) is that all data is read from sys.stdin, which reads strings, not bytes. This obviously can't work properly to upload binary files. In the proposed version, for multipart/form-data type, all data is read as bytes from sys.stdin.buffer ; in the CGI script, the Python interpreter must be launched with the -u option, as suggested by Amaury, otherwise sys.stdin.buffer.read() only returns the beginning of the data stream

The headers inside the multipart/form-data are decoded to a string using sys.stdin.encoding and passed to a FeedParser (which requires strings) ; then the data is read from sys.stdin.buffer (bytes) until a boundary is found

If the field is a file, the file object in self.file stores bytes, and the attribute "value" is a byte string. If it is not a file, the value is decoded to a string, always using sys.stdin.encoding, as for all other fields for other types of forms

Other cosmetic changes :
- replaced "while 1" by "while True"
- replaced "if type(value) == type([])" by "if isintance(value,list)"

Attached file : zip with cgi_new.py and tests in a folder called "http"
Tested with Python 3.2b2 (r32b2:87398, Dec 19 2010, 22:51:00) [MSC v.1500 32 bit (Intel)] on win32 ; files and CGI scripts served by Apache 2.2
History
Date User Action Args
2011-01-02 08:51:42quentelsetrecipients: + quentel, gvanrossum, barry, amaury.forgeotdarc, ggenellina, giampaolo.rodola, eric.araujo, v+python, r.david.murray, oopos, tercero12, tcourbon, tobias, flox, l0nwlf, pebbe
2011-01-02 08:51:42quentelsetmessageid: <1293958302.14.0.785429236239.issue4953@psf.upfronthosting.co.za>
2011-01-02 08:51:37quentellinkissue4953 messages
2011-01-02 08:51:37quentelcreate