python - 如何上传大(~100Mb)文件?
问题描述
我有以下烧瓶应用程序
文件格式.html:
<html>
<head>
<title>Simple file upload using Python Flask</title>
</head>
<body>
<form action="/getSignature" method="post" enctype="multipart/form-data">
Choose the file: <input type="file" name="photo"/><BR>
<input type="submit" value="Upload"/>
</form>
</body>
</html>
应用程序.py:
import os
from flask import Flask, request, render_template, url_for, redirect
app = Flask(__name__)
@app.route("/")
def fileFrontPage():
return render_template('fileform.html')
@app.route("/getSignature", methods=['POST'])
def handleFileUpload():
if 'photo' in request.files:
photo = request.files['photo']
if photo.filename != '':
filepath = os.path.join('/flask/files', photo.filename)
photo.save(filepath)
return render_template('result.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
这适用于较小的文件,但不适用于较大的文件。点击上传按钮后,浏览器显示(Uploading 13%..)
,然后浏览器超时ERR_CONNECTION_RESET
。在烧瓶应用程序中看不到任何错误。
我通过 Nginx 提供服务。当我检查 nginx 日志时,我看到
2020/02/20 22:38:47 [error] 6#6: *170614 client intended to send too large body: 80762097 bytes, client: 10.2.16.178, server: localhost, request: "POST /getSignature
我需要为此添加任何 Nginx 配置吗?
我想上传最大 100Mb 的文件。
解决方案
内容长度可能被限制为默认值。
当您使用 nginx 时,可能是它玩游戏的上游超时(默认为 upstream keepalive_timeout 60s
)将以下设置添加到 nginx:
keepalive_timeout 900s;
#extra
proxy_read_timeout 900s;
uwsgi_read_timeout 900s;
更多调音在这里
如果你使用像 uwsgi 这样的 python wsgi 服务器,还要在那里设置keep alive:
http-keepalive 900;
推荐阅读
- mathematical-optimization - 如何使用 SCIP 解决 SAT 问题?
- ios - 如何使用 UITableViewCell 中的委托方法在标签中设置计算?
- android - 引起:net.sqlcipher.database.SQLiteException: file is not a database: ,编译时:select count(*) from sqlite_master
- reactjs - 将参数传递到后台屏幕并在本机反应中设置状态
- linux - Nexus 服务立即停止 2
- reactjs - 如何根据git分支更改变量值?
- azure-devops - VS403363:您的个人帐户无权访问此内容。Azure 开发运维
- python - 使用 HDBSCAN 的可调用指标*
- java - 使用字符串而不是使用单个字符的二叉搜索树高度
- python - 在 Robot Framework 的 websocketclient 库中传递子协议