python - 使用 curl 将文件上传到 python flask 服务器

标签 python curl flask

我正在尝试使用 curl 和 python flask 将文件上传到服务器。下面我有我如何实现它的代码。关于我做错了什么的任何想法。

curl -i -X PUT -F name=Test -F filedata=@SomeFile.pdf "http://localhost:5000/" 

@app.route("/", methods=['POST','PUT'])
def hello():
    file = request.files['Test']
    if file and allowed_file(file.filename):
        filename=secure_filename(file.filename)
        print filename

    return "Success"

以下是服务器发回的错误

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

提前致谢。

最佳答案

您的 curl 命令意味着您正在传输两个表单内容,一个名为 filedata 的文件,一个名为 name 的表单字段。所以你可以这样做:

file = request.files['filedata']   # gives you a FileStorage
test = request.form['name']        # gives you the string 'Test'

request.files['Test'] 不存在。

关于python - 使用 curl 将文件上传到 python flask 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329087/

相关文章:

python - python中**ASTERISK变量**的语义

python - 在 scikit-learn 中实现 K Neighbors Classifier 和 Linear SVM 以进行词义消歧

python - 如何 reshape numpy 数组并保留每个第 n 个元素

curl - 如何使用curl喜欢YouTube视频?

PHP:使用远程 URL 上传 CurlFile

java - 带有 cookies/session 的 HttpClient 4.x httpGet

python - 将 Flask 应用程序部署到 Heroku - "Connection in Use"

python - 需要 float __len__,而不是整数

python - Flask的url_for不引用斜杠

python - Flask:如何通过点击链接来更改html中flask变量的值