【发布时间】:2019-02-27 20:21:46
【问题描述】:
我有一个 curl 命令,它将转换为 base64 的 jpg 图像发送到网络服务:
curl -X POST --insecure \
https://link_to_the_web_service.com \
-H 'authorization:authorization_token' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{"model_spec": {"name": "inception", "signature_name": "predict_images"}, "inputs": {"images": {"dtype": 7, "tensor_shape": {"dim":[{"size": 1}]}, "string_val": ["IMAGE_CONVERTED_TO_BASE_64"]}}}'
我正在使用以下网站将 jpg 图像转换为 base64:“https://www.browserling.com/tools/image-to-base64”。 curl命令的执行给出了成功的输出。
现在我正在通过获取 jpg 图像、将 curl 命令转换为 python3 并使用 python3 将图像转换为 base64 来测试 Web 服务,如下所示:
import requests
import base64
host = 'https://link_to_the_web_service.com'
image = 'sample5.jpg'
image_64_encode = base64.b64encode(open('sample5.jpg',"rb").read())
headers = {'authorization': token, 'cache-control': 'no-cache', 'content-type': 'application/json'}
data={"model_spec": {"name": "inception", "signature_name": "predict_images"}, "inputs": {"images": {"dtype": 7, "tensor_shape": {"dim":[{"size": 1}]}, "string_val": [str(image_64_encode)]}}}
request = requests.post(url=host,
data=data,
headers=headers,
verify=False)
print(request)
我收到 响应,这意味着 Web 服务无法读取图像输入。 我什至尝试将 'base64.encodestring' 转换为 base64,但没有成功。
如何正确地将上述 curl 命令转换为 python?
【问题讨论】:
标签: python python-3.x curl python-requests base64