当文件很大时,Flask会重置连接,而不是返回413 [英] Flask resets the connection instead of returning a 413, when the file is to big

查看:52
本文介绍了当文件很大时,Flask会重置连接,而不是返回413的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义 MAX_CONTENT_LENGTH 时,Flask应该返回413,但在我的情况下,它将重置连接并出现以下错误:错误101(net :: ERR_CONNECTION_RESET):连接已重置.

When MAX_CONTENT_LENGTHis definded, Flask should return an 413, but in my case it resets the connection with this error: Error 101 (net::ERR_CONNECTION_RESET): The connection was reset.

我附上了一个基于Flask文件上传模式的示例.

I attached a example built upon the file upload pattern from Flask.

我使用的是Chrome 15.0.874.5.

I use Chrome 15.0.874.5.

import os
from flask import Flask, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename

UPLOAD_FOLDER = 'uploads/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','bib','mp3'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)

@app.errorhandler(413)
def file_to_big(e):
    return 'File to big'

if __name__ == '__main__':
    app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024
    app.run(debug=True)                    

推荐答案

使用Flask Dev服务器似乎只是一个问题.当我进行lighttp直播时,重定向有效.

It seems that it is only a problem using the Flask Dev sever. When I went live on a lighttp the redirect worked.

这篇关于当文件很大时,Flask会重置连接,而不是返回413的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆