我正在尝试合并上传一个基本的文本/ csv文件到我的web应用程序,该应用程序运行烧瓶来处理http请求。我试图按照localhost here上运行的烧瓶文档中的宝宝示例。但是,当我在我的页面上尝试这段代码时,它似乎上传,但后来只是挂起,事实上我的瓶服务器冻结,我不得不关闭终端再试一次... Ctrl + C甚至不工作。在python烧瓶中上传文件
我执行run.py
:
#!/usr/bin/env python
from app import app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False)
和app
是在同一目录的目录中run.py
与以下__init__.py
:
import os
from flask import Flask
from werkzeug import secure_filename
#Flask object initialization
#app flask object has to be created before importing views below
#because it calls "import app from app"
UPLOAD_FOLDER = '/csv/upload'
ALLOWED_EXTENSIONS = set(['txt', 'csv'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
,这里是我的views.py
文件,其中有我的所有路由:
from flask import render_template, request, redirect, url_for
from app import app
import os
#File extension checking
def allowed_filename(filename):
return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
@app.route('/index.html', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
submitted_file = request.files['file']
if submitted_file and allowed_filename(submitted_file):
filename = secure_filename(submitted_file.filename)
submitted_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>
'''
有人可以告诉我字符串“uploaded_file”来自哪里吗? – computingfreak