【发布时间】:2015-07-26 05:58:15
【问题描述】:
我正在寻找一种从网络获取图像并将其返回给客户端的方法(无需先保存到磁盘)。类似的东西(取自here):
import requests
from flask import Response, stream_with_context
@files_blueprint.route('/', methods=['GET'])
def get_image():
req = requests.get('http://www.example.com/image1.png', stream = True)
return Response(stream_with_context(req.iter_content()), content_type = req.headers['content-type'])
上面的代码可以运行,但是速度很慢。
有更好的方法吗?
【问题讨论】:
-
哪一部分慢?
requests.get或return Response? -
您可以在
iter_content(chunk_size)中尝试使用不同的尺寸。默认大小是1,这会很慢。请改用1024或2048。 -
我将 chunk_size 更改为 2048 - 好多了!谢谢!要将其发布为答案以便我标记它吗?
标签: python flask python-requests