Upload of binary files

Steve Holden sholden at holdenweb.com
Sun Mar 16 17:51:20 CET 2003


"Peter Mott" <pmott at comp.leeds.ac.uk> wrote in message
news:HBr4EA.oDB at leeds.ac.uk...
> The problem described by myself has been solved in Ben Hutchings post: The
> code belows works as we would wish - the binary file is uploaded and the
> number of bytes returned to the client:
>
>
>
> try:
>     import msvcrt, os
>     msvcrt.setmode(0, os.O_BINARY) # stdin = 0
>     msvcrt.setmode(1, os.O_BINARY) # stdout = 1
> except ImportError:
>     pass
>
> import cgi
> import cgitb; cgitb.enable()
>
> form = cgi.FieldStorage()
>
> if form.has_key("Data"):
>     fileinfo=form["Data"]
>     chunk = fileinfo.file.read()
>
>
> print "Content-type: text/plain\n\n"
> print "Bytes read:",len(chunk)
>
>
> The point is that without the import of msvcrt (providing low level
Windows
> support) the cgi module tries to treat all files as text and so the
uploads
> do not work. I am not a seasoned Python person but I don't think the above
> code is very 'Pythonic' but for me it is a perl none the less. Thanks Ben!
>

One last thing you might try if your web server honors the shebang
convention (as Xitami and Apache both do, for example, even on Windows) is
to use a first line similar to

#!C:/python22/python -u

This will cause Python to run unbuffered, and should obviate the need to
explicitly use the mscvrt module.

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Register for PyCon now!            http://www.python.org/pycon/reg.html







More information about the Python-list mailing list