通过开机自检请求下载文件



>我有一个 API 端点,它根据一些传递数据动态生成图像。我想调用 API 并将响应下载到文件中。在 Python 中实现此目的的最佳方法是什么?

该请求在 cURL 中如下所示:

curl https://localhost:4000/bananas/12345.png 
  -O 
  -X POST 
  -d '[ 1, 2, 3, 4 ]'

你应该使用requests包而不是执行curl或其他进程:

import requests
response = requests.post('https://localhost:4000/bananas/12345.png', data = '[ 1, 2, 3, 4 ]')
data = response.content

data包含之后下载的内容,例如,您可以将其存储到磁盘:

with open(path, 'wb') as s:
    s.write(data)

我更喜欢subprocess而不是os.system任何一天,所以这里是等效的:

import subprocess
_CURL_POST = "curl https://localhost:4000/bananas
/12345.png 
-O 
-X POST 
-d '[ 1, 2, 3, 4 ]'
"
subprocess.call(_CURL_POST)

最新更新