REST服务

基于Python语言的开发

安装依赖包

pip install flask
pip install flask_restful

上传图片

import tempfile
import werkzeug

from flask import Flask
from flask_restful import reqparse, abort, Api, Resource


class UploadImage(Resource):
    def post(self):
        parse = reqparse.RequestParser()
        parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
        args = parse.parse_args()

        img_file = args['file']

        if not img_file:
            return {'no file'}, 417

        with tempfile.NamedTemporaryFile() as f:
            img_data = args['file'].read()
            f.write(img_data)

        return detect_result, 201


if __name__ == '__main__':
    app = Flask(__name__)
    api = Api(app)

    api.add_resource(UploadImage, '/upload_image')

    app.run(host=0.0.0.0, debug=False)

curl测试

curl -v -X POST -H "Content-Type: multipart/form-data" -F "file=@test.jpg" http://127.0.0.1:5000/upload_image

参考资料

results matching ""

    No results matching ""