在json-rpc调用中打印帖子的状态代码

时间:2018-04-26 15:44:04

标签: python json request http-status-codes json-rpc

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

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()

1 个答案:

答案 0 :(得分:0)

.json()返回后,您立即致电.post()。这意味着您将从响应中丢弃其余信息。

请改为尝试:

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/