例如,这个网址:
http://example.com/get_image?type=1
应该返回带有 image/gif
MIME 类型的响应。我有两个静态 .gif
图片,
如果 type 是 1,它应该返回 ok.gif
,否则返回 error.gif
。如何在 flask 中做到这一点?
最佳答案
你使用类似的东西
from flask import send_file
@app.route('/get_image')
def get_image():
if request.args.get('type') == '1':
filename = 'ok.gif'
else:
filename = 'error.gif'
return send_file(filename, mimetype='image/gif')
返回 ok.gif
或 error.gif
,具体取决于类型查询参数。请参阅 send_file
function 的文档。和 request
object了解更多信息。
关于python - 如何在 flask 响应中返回图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637153/