Pierre Quentel <pierre.quen...@gmail.com> added the comment:

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

----------
nosy: +quentel
Added file: http://bugs.python.org/file20217/http.zip

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue4953>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to