python - 在使用 Flask uwsgi 提供 PIL 图像时尝试隐式序列转换
问题描述
我正在通过 Flask 提供 PNG 或 SVG 图像。在本地它工作正常,但是当我在 docker 内运行应用程序并发送请求(POST)时,我收到以下错误:
RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
通过烧瓶提供 PIL 图像的波纹管代码:
def serve_image(image: Image, mime_type: FileFormat, download: bool):
suffix = mime_type.value.split('/')[-1]
temp_file = tempfile.TemporaryFile(mode='w+b', suffix=suffix)
if suffix == 'png':
image.save(temp_file, suffix)
else:
# we cant force svg extension in PIL
image.save(temp_file)
temp_file.seek(0, 0)
return send_file(temp_file, mimetype=mime_type.value, as_attachment=download,
attachment_filename='img.' + suffix)
我尝试过使用 BytesIO 也没有运气。环境
Response.implicit_sequence_conversion = False
Response.direct_passthrough = False
或者
@app.after_request
def after_request_func(r):
r.direct_passthrough = False
r.implicit_sequence_conversion = False
return r
也没有帮助。
解决方案
问题出在 openapi-core Flask 验证中,通过创建werkzeug Response 并将 direct passtrough 设置为 False 解决了这个问题。必须手动设置附件缓存超时等其他标头。
res = Response(temp_file, direct_passthrough=False, mimetype=mime_type.value)
推荐阅读
- networking - 警告:没有类变量 LanRouter::debug_
- javascript - React Native - 编辑代码推送目标/版本号(在应用程序中)
- intel-mkl - 在运行 macOS Catalina 的 Mac 上构建 Kaldi
- java - Flutter WebView在html选择上崩溃
- c# - C#、SQL 服务器。提供者:TCP 提供者,错误:0 - 不知道这样的主机
- node.js - 将 vue 应用程序部署到 heroku 时出错
- c# - IFormFile 在 ASP.NET Core 3.0 中返回 NULL
- javascript - 我们可以在 Axios 中做一个 If - ReactJS
- python - 属性错误:“ReduceLROnPlateau”对象没有属性 _cmp
- java - Android - 创建自定义 xml 属性,如 onClick?