Flask HTTP服务器正在运行server.py
:
import os, io, zipfile, time
from flask import Flask, request, send_file
app = Flask(__name__)
FILEPATH = '/change/it/to/any_file.path'
@app.route('/download', methods=['GET','POST'])
def download():
fileobj = io.BytesIO()
with zipfile.ZipFile(fileobj, 'w') as zip_file:
zip_info = zipfile.ZipInfo(FILEPATH)
zip_info.date_time = time.localtime(time.time())[:6]
zip_info.compress_type = zipfile.ZIP_DEFLATED
with open(FILEPATH, 'rb') as fd:
zip_file.writestr(zip_info, fd.read())
fileobj.seek(0)
return send_file(fileobj.read(), mimetype='zip', as_attachment=True, attachment_filename = '%s.zip' % os.path.basename(FILEPATH))
app.run('0.0.0.0', 80)
client.py
发送下载文件的请求:
server
引发TypeError
:
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
如何解决这个问题?在
其中一个可能的解决方案将使用
Flask.make_response
方法:相关问题 更多 >
编程相关推荐