【问题标题】:why sending image in python using form with post is not working为什么使用带有帖子的表单在python中发送图像不起作用
【发布时间】:2019-07-12 09:23:50
【问题描述】:

尝试使用 formspost 使用 Requests 包将图像发送到 Flask 服务器。但是烧瓶服务器无法解析密钥image。如何通过使用表单的请求以正确的格式发送图像。

Flask server.py

@app.route('/api/<model_name>/predict/', methods=['POST'])
def predict(model_name):

    if "image" in request.files.keys():
        return jsonify({"msg": "key found"})

    print("image", request.files)
    return str(), 200

请求 client.py

def get_predictions(path):

   url = "http://localhost:9020/api/fasterrcnn/predict/"

   payload = {"image": (path.name, open(path, "rb"), "image/jpeg")}
   headers = {'content-type': "multipart/form-data"}
   response = requests.post(
       url, data=payload, headers=headers, stream=True)

   pprint(response.text)

谁能告诉我可能的原因和解决方案?如果我遗漏了任何内容,过分或过分强调了某个特定点,请在 cmets 中告诉我。

【问题讨论】:

    标签: python forms post flask python-requests


    【解决方案1】:

    requests documentation page 声明您应该使用files= 参数来发布多部分文件。

    例子:

    import requests
    
    def get_predictions(path):
       url = "http://localhost:9020/api/fasterrcnn/predict/"
    
       files = {"image": (path.name, open(path, "rb"), "image/jpeg")}
       response = requests.post(url, files=files)
    
       pprint(response.text)
    

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多