Help: Uploading .zip to Python CGI

Robin Munn rmunn at pobox.com
Mon Dec 8 13:52:04 EST 2003


Will Stuyvesant <hwlgw at hotmail.com> wrote:
> I am uploading a .zip file to a Python CGI program, using a form on
> a HTML page with
>
>     <input name="yourfile" type="file">...
>
> In the Python CGI program I do:
>
> import cgi
> fStorage = cgi.FieldStorage()
> zipdata = fStorage['yourfile'].value
> print "Content-type: text/plain"
> print
> print len(zipdata)
>
> Now the length of the zipdata is 100, where it should be about
> 2635...and unzipping it with zipfile of course gives the "not a zip
> file" error.
>
> The last part of the data that is received by the CGI script is:
>
> \xf2\xf1!0\xdbS\xa9
>
> and the next one *should* be \x1a

\x1a is ASCII for Ctrl-Z, which is used in Windows as an EOF (End Of
File) marker.

> It seems that the .zip data is being truncated, but I don't know where
> in my tool chain.

Somewhere in your tool chain, something is opening a file in text mode
instead of in binary mode.

The fact that your CGI script works on Unix but fails on Windows is
further proof that the Ctrl-Z is being treated as EOF on Windows, since
Unix doesn't give that character any special meaning.

-- 
Robin Munn
rmunn at pobox.com




More information about the Python-list mailing list