在 json-rpc 调用中打印来自 post 的状态代码

Print status code from the post in a json-rpc call

我有一个工作脚本可以很好地完成工作,但我似乎无法弄清楚如何在脚本运行后打印状态代码。 有人可以审查并提供一些指导和帮助吗?

import requests
import json


url = 'http://10.3.198.100/ins'
switchuser = 'user'
switchpassword = 'password'

myheaders = {'content-type' : 'application/json-rpc'}
payload = [
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
    "cmd": "vrf context management",
    "version": 1
    },
    "id": 1
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
    "cmd": "ip route 192.168.255.0/24 10.3.198.130",
    "version": 1
  },
    "id": 2
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
    "cmd": "copy run start",
    "version": 1
    },
    "id": 3
  }
 ]
 response = requests.post(url, data = json.dumps(payload), headers = myheaders, auth = (switchuser, switchpassword)).json()

您在 .post() returns 之后立即调用 .json()。这意味着您将丢弃响应中的其余信息。

试试这个:

response = requests.post(
    url,data=json.dumps(payload),
    headers=myheaders,
    auth=(switchuser,switchpassword))
json_response = response.json()
print(response.status_code)

参考:http://docs.python-requests.org/