I am running a Python 2.7 script as CGI on Apache 2.4 on Win 10, the sctipt sends a ZIP archive as download in HTTP response. I followed this thread How to deploy zip files (or other binaries) trough cgi in Python?, but keep getting a broken ZIP file. I hope someone can help as I've been trying to resolve this for 2 days, cannot find any info on this behavior.
Demo Script:
import cgi, cgitb, os
import shutil
cgitb.enable()
out_path = os.path.dirname(__file__) + "\\tmp_uploads\\test2.zip"
# send output zip as download
import sys
print "Content-Disposition: attachment; filename=\"test2.zip\""
print "Content-Type: application/zip"
print
##sys.stdout.flush()
with open(out_path,'rb') as zf:
shutil.copyfileobj(zf, sys.stdout)
## print zf.read()
Enabling sys.stdout.flush()
or using print zf.read()
instead of shutil.copyfileobj(zf, sys.stdout)
makes no difference.
Original ZIP file is intact:
Downloaded archive is broken: