问题
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?
回答1:
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
.
回答2:
Use mod_wsgi instead of cgi. It will provide you an input file for the upload that's correctly opened.
来源:https://stackoverflow.com/questions/838991/python-cgi-and-stdin