Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top