tags:

views:

541

answers:

2

I'm using pycurl to upload a file via put and python cgi script to receive the file on the server side. Essentially, the code on the server side is:

while True:
   next = sys.stdin.read(4096)
   if not next:
       break
   #.... write the buffer

This seems to work with text, but not binary files (I'm on windows). With binary files, the loop doing stdin.read breaks after receiving anything around 10kb to 100kb. Any ideas?

+2  A: 

You need to run Python in binary mode. Change your CGI script from:

#!C:/Python25/python.exe

or whatever it says to:

#!C:/Python25/python.exe -u

Or you can do it programmatically like this:

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

before starting to read from stdin.

RichieHindle
Shouldn't that final slash be a period?
unwind
@unwind: Oops, well spotted. Now fixed.
RichieHindle
A: 

Use mod_wsgi instead of cgi. It will provide you an input file for the upload that's correctly opened.

S.Lott