Python 如何使用Flask将send_file()与1个响应中的另一个信息(例如int)结合起来
我的问题是,我试图使用Python和Flask向客户机和图像发送另一个附加信息 我尝试使用send_file(),但问题是我只能发送图像,无法找到其他方法发送附加信息。 我还尝试将图像和信息合并到JSON中,但似乎无法将send_file()序列化为JSONPython 如何使用Flask将send_file()与1个响应中的另一个信息(例如int)结合起来,python,flask,Python,Flask,我的问题是,我试图使用Python和Flask向客户机和图像发送另一个附加信息 我尝试使用send_file(),但问题是我只能发送图像,无法找到其他方法发送附加信息。 我还尝试将图像和信息合并到JSON中,但似乎无法将send_file()序列化为JSON def post(self): img_path, score = self.get_result() final_image = send_file( img_path, mimetype=
def post(self):
img_path, score = self.get_result()
final_image = send_file(
img_path,
mimetype='image/jpg'
)
output = {'img': final_image, 'score': score}
return output
<> P>是否有任何方法可以在客户请求1的范围内收到额外的结果? < P>你可以考虑以下任何一种方法:
- 将额外信息设置为cookie
- 设置其他响应标题以包含额外信息李>
- 或者在JSON响应中对文件内容进行编码。您可以编码为base64字符串或使用(服务器端库:,客户端库:)
你可以考虑以下任何一种方法:
- 将额外信息设置为cookie
- 设置其他响应标题以包含额外信息李>
- 或者在JSON响应中对文件内容进行编码。您可以编码为base64字符串或使用(服务器端库:,客户端库:)
response = send_file(
img_path,
mimetype='image/jpg'
)
response.set_cookies('score', score)
response = send_file(
img_path,
mimetype='image/jpg'
)
response.set_header('x-myapp-score', score)
from base64 import b64encode
import logging
logger = logging.getLogger(__name__)
def post(self):
# ...
output = {
'score': score
}
try:
with open(final_image, 'rb') as f:
content = f.read()
output['img'] = b64encode(content)
except TypeError, FileNotFoundError:
# handle default image ¯\_(ツ)_/¯
logger.exception('Failed to encode image file')
return output