使用场景:flask 返回二进制文件
服务器端
from flask import Flask, send_file
from pathlib import Path
app = Flask(__name__)
@app.route('/')
def home():
return "hello world"
@app.route('/download')
def download():
file = Path(__file__).parent / "fileMode.mp4"
print("===============================")
if not file.exists():
raise FileNotFoundError
print('file path: ', file.absolute())
return send_file(str(file.absolute()))
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8888, debug=True)
客户端
import requests
if __name__ == '__main__':
proxies = {
"http": None,
"https": None,
}
response = requests.get('http://127.0.0.1:8888/download', proxies=proxies)
with open("download.mp4", mode='wb') as f:
f.write(response.content)