Alternative to python -u for binary upload to cgi on windows?

C

Cameron Walsh

Hi all,

Using a python cgi script such as the one below to handle uploaded
binary files will end up with a truncated file (truncates when it hits
^Z) on Windows systems. On linux systems the code works and the file is
not truncated.

One solution for Windows is to use the -u flag, i.e.
#!C:\Python\python.exe -u

Is there another fix that doesn't require python to be run in unbuffered
mode? What performance hits would I expect for running in unbuffered mode?

Best regards,

Cameron.


Example code:

import cgi, os.path as path
form = cgi.FieldStorage()
new_file = form.getvalue("new_file",None)
if new_file is not null:
fname = path.split(form["new_file"].filename)[-1]
fle = open(fname,"wb")
fle.write(new_file)
fle.close()
 
C

cameron.walsh

Hi all,

Using a pythoncgiscript such as the one below to handle uploaded
binary files will end up with atruncatedfile (truncates when it hits
^Z) on Windows systems. On linux systems the code works and the file is
nottruncated.

One solution for Windows is to use the -u flag, i.e.
#!C:\Python\python.exe -u

Is there another fix that doesn't require python to be run in unbuffered
mode? What performance hits would I expect for running in unbuffered mode?

Answer to the first part:

import msvcrt, sys, os
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

# other problems may also be fixed with:
# msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

Cameron.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top