作为HTTP响应发送时,Python 2.7 ZIP归档文件被破坏

作为HTTP响应发送时,Python 2.7 ZIP归档文件被破坏,python,python-2.7,apache,http,Python,Python 2.7,Apache,Http,我在Win10上的Apache2.4上以CGI的形式运行Python 2.7脚本,sctipt在HTTP响应中以下载的形式发送ZIP存档。我按照这条线索,但不断得到一个损坏的ZIP文件。我希望有人能帮助我,因为我已经尝试解决这个问题两天了,找不到任何关于这个行为的信息 演示脚本: import cgi, cgitb, os import shutil cgitb.enable() out_path = os.path.dirname(__file__) + "\\tmp_uploa

我在Win10上的Apache2.4上以CGI的形式运行Python 2.7脚本,sctipt在HTTP响应中以下载的形式发送ZIP存档。我按照这条线索,但不断得到一个损坏的ZIP文件。我希望有人能帮助我,因为我已经尝试解决这个问题两天了,找不到任何关于这个行为的信息

演示脚本:

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()
启用
sys.stdout.flush()

原始ZIP文件完好无损:

下载的存档文件已损坏: