【发布时间】:2020-10-24 00:32:06
【问题描述】:
我在 Win 10 上的 Apache 2.4 上将 Python 2.7 脚本作为 CGI 运行,sctipt 在 HTTP 响应中发送一个 ZIP 存档作为下载。我关注了这个线程How to deploy zip files (or other binaries) trough cgi in Python?,但不断收到损坏的 ZIP 文件。我希望有人可以提供帮助,因为我已经尝试解决此问题 2 天,但找不到有关此行为的任何信息。
演示脚本:
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() 或使用print zf.read() 代替shutil.copyfileobj(zf, sys.stdout) 没有区别。
原始 ZIP 文件完好无损:
下载的存档损坏:
【问题讨论】:
标签: python python-2.7 apache http