简体   繁体   English

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

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

I have a working script that does the job just fine but I can't seem to figure out how to print the status code after the script runs. 我有一个可以正常工作的脚本,但是我似乎无法弄清楚在脚本运行后如何打印状态代码。 Can someone please review and provide some guidance and help? 有人可以查看并提供一些指导和帮助吗?

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

You are immediately calling .json() after your .post() returns. 您的.post()返回后,您将立即调用.json() This means that you are throwing away the rest of the info from the response. 这意味着您将丢弃响应中的其余信息。

Try this instead: 尝试以下方法:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM