python - 烧瓶 send_file 和 send_from_directory 导致会话 cookie 太大
问题描述
嗨,这是一个有点奇怪的问题。运行时它不会在我的本地服务器上发生,flask run
但是当使用 gunicorn 和 nginx 烧瓶send_file()
方法或send_from_directory()
我用来允许用户下载 .pdf 文件时,我会因以下错误而崩溃:
/home/ben/newvenv/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py:479: UserWarning: The "b'session'" cookie is too large: the value was 10004 bytes but the header required 26 extra bytes. The final size was 10030 bytes but the limit is 4093 bytes. Browsers may silently ignore cookies larger than this.
samesite=samesite,
这是我调用此方法的代码:
return send_from_directory(directory= directory,filename=filename, as_attachment=True)
我尝试更新我的 nginx 配置以允许更大的 cookie,但这不起作用,也不是一个理想的解决方案。我错过了什么?这是 nginx 的问题还是我调用烧瓶方法的方式?.pdf 文件不大,只有十页长。
解决方案
我通过在发送文件之前清除会话来解决这个问题。这是我用来清除会话变量的代码:
def clear_session_variables(exclude=[]):
"""deletes all session variables. Useful to reset before starting new task."""
print(list(session.keys()))
for key in list(session.keys()):
if(key != '_flashes' and key != 'csrf_token' and key not in exclude):
print(key)
session.pop(key)
print(list(session.keys()))
这是我调用该方法的地方:
clear_session_variables(exclude=['db','historical'])
return send_from_directory(directory= directory,filename=filename, as_attachment=True, mimetype='application/pdf')
推荐阅读
- reactjs - React 解构:这种语法的含义是什么?
- octobercms - 如何查看十月翻译的县CMS rainlab.location 插件
- java - Java 动态绑定失败
- dynamics-crm - 在 Dynamics 365 9.0 上将 PSA 升级到 2.4.x
- python - print(x) TypeError: 'str' 对象不可调用
- c# - Unity Package Manager 窗口在 2018.2 中不可用。?
- python - Scrapy 中间件返回响应
- angularjs - Angular-DataTables:通过使用 Promise 渲染 + 角度绑定 + 行索引 + groupBy 来提高性能
- vue.js - @vue/cli 使用我的库构建应用程序失败
- powerbi - 比较列值与另一个表中的度量(散点图突出显示)