flask 问题记录
1、flask request传递发送文件数据。
前端发送文件数据到一个服务,由此服务转发到另外一个服务。
requests.request 方法 files 参数说明
files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
# 从前端获取文件列表数据:
file_list = request.files.getlist("file")
for file in file_list:
content = file.read()
files_dict[file.filename] = (file.filename, content, file.content_type)
# 从form表单获取数据:
data = request.form.to_dict()
# 转发files数据
response = requests.request(method, endpoint + url, headers=headers, files=files_dict, data=data)
2、db session 超时
在异步处理数据时,如果进入异步操作时,没有执行db.session.close(),可能导致数据库长连接而报错。
因此在进入长时间的异步操作,请记住关闭当前数据库连接。
3、flask tls 启动 版本 加密算法设置
4、使用chardet解码大文件可能内存泄露,时间超长。建议使用cchardet。