使用curl将文件上传到python flask服务器 [英] Upload a file to a python flask server using curl

查看:2380
本文介绍了使用curl将文件上传到python flask服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用curl和python烧瓶将文件上传到服务器。下面我有我如何实现它的代码。任何想法,我做错了。

I am trying to upload a file to a server using curl and python flask. Below I have the code of how I have implemented it. Any ideas on what I am doing wrong.

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"

以下是服务器发回的错误

The following is the error that the server sends back

<!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 的文件和一个名为名称的表单字段。所以你可以这样做:

Your curl command means you're transmitting two form contents, one file called filedata, and one form field called name. So you can do this:

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

$ c> request.files ['Test'] 不存在。

but request.files['Test'] doesn't exist.

这篇关于使用curl将文件上传到python flask服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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