flask_request.py 683 B

1234567891011121314151617181920212223
  1. # 启用flask_start的服务后,将数据以post的方式调用服务得到结果
  2. import json
  3. import base64
  4. import requests
  5. def image_encode(image_path):
  6. with open(image_path, 'rb')as f:
  7. image_byte = f.read()
  8. image_base64 = base64.b64encode(image_byte)
  9. image = image_base64.decode()
  10. return image
  11. if __name__ == '__main__':
  12. url = 'http://0.0.0.0:9999/test/' # 根据flask_start中的设置: http://host:port/name/
  13. image_path = 'demo.jpg'
  14. image = image_encode(image_path)
  15. request_dict = {'image': image}
  16. request = json.dumps(request_dict)
  17. response = requests.post(url, data=request)
  18. result = response.json()
  19. print(result)