Flask Restful读取文件

时间:2018-06-06 13:05:54

标签: python api flask flask-restful

我遇到了一个困难:我想访问我发布的请求文件的内容。

client.py

url = 'http://127.0.0.1:5000/sendimage/test.jpg'
filename = url.split('/')[-1]
print filename
files = {'media': open(filename, 'rb')}
response = requests.post(url, files=files)

server.py

from flask import Flask, request
from flask_restful import Api, Resource, reqparse
import json

app = Flask(__name__)
api = Api(app)

class sendImage(Resource):

    def post(self, pathImg):
        print "Receiving Image ..."
        # Extraction of the sent file
        return "null"

api.add_resource(sendImage, '/sendimage/<pathImg>')

if __name__ == '__main__':
    app.run(port='5000')

问题是: server.py如何读取client.py ??

发送的文件中的数据

1 个答案:

答案 0 :(得分:0)

它应该在request.files['media']

中提供

您可以阅读有关上传文件的完整文档,其中包含详尽的文档http://flask.pocoo.org/docs/1.0/patterns/fileuploads/