【发布时间】:2020-04-19 17:40:29
【问题描述】:
我正在尝试将 Curl POST 请求转换为 python 脚本,但我没有得到所需的输出,请让我知道我在这里做错了什么。
CURL request
curl -s -w '%{time_starttransfer}\n' --request POST \
--url http://localhost:81/kris/execute \
--header 'content-type: application/json' \
--data '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
这会在为其提供 ip 的节点中运行 uptime 命令并返回 JSON 输出:
{"command":"uptime","output":["{\"body\":\" 17:30:06 up 60 days, 11:23, 1 user, load average: 0.00, 0.01, 0.05\\n\",\"host\":\"10.0.0.1\"}"]}0.668894
当我尝试用 python 运行相同的东西时,它会失败并且永远不会得到输出
代码:
import urllib3
import json
http = urllib3.PoolManager()
payload = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
encoded_data = json.dumps(payload)
resp = http.request(
'POST',
'http://localhost:81/kris/execute ',
body=encoded_data,
headers={'Content-Type': 'application/json'})
print(resp)
【问题讨论】:
标签: python-3.x curl urllib3