我试图制作小型休息网络服务,只能通过curl查询使用。我坚持上传文件。这是我的代码(可能不需要其中一些导入)
#!bin/python
import os
from flask import Flask
from flask import jsonify
from flask import request
from flask import Response
from flask import redirect
from flask import url_for
from flask import flash
from flask import session as Session
from werkzeug import create_environ
from werkzeug.utils import secure_filename
app = Flask(__name__)
UPLOAD_FOLDER = '~/testUpload/'
ALLOWED_EXTENSIONS = set(['xml'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
environ = create_environ('/','127.0.0.1:5000')
response = Response()
SESSION_TYPE = 'filesystem'
def allowed_file(filename):
'''
'''
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET','POST'])
def executeUploadFile():
'''
'''
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('index'))
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>
<p>%s</p>
""" % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],))
if __name__ == '__main__':
app.config['SESSION_TYPE'] = SESSION_TYPE
app.secret_key = 'aaaa'
app.run(debug=True)
当我试图执行时 curl -X POST -F&#34; file=@test.xml" &#34;本地主机:5000 /上传/&#34;
它只返回错误的请求。我有什么建议我做错了吗? 感谢