[Python]Curl命令模拟与json解析

核心:

  • 利用os.popen来执行curl请求
  • 利用json.loads来解析数据

以解析zabbix 登录token为例:

import os
import json

curl_login = """curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"XXX"},"auth":null,"id":0}' http://127.0.0.1/zabbix/api_jsonrpc.php"""
# 样例输出 {"jsonrpc":"2.0","result":"b8e3192e3405c805cd9527f0a1234567","id":0}
res_login = os.popen(curl_login).read()
res_login_body = res_login.split("\n")[-1]
res_login_json = json.loads(res_login_body)
token = res_login_json["result"]

你可能感兴趣的