我是 Python 新手,我想做的是编写一个脚本,该脚本将获取我提供的 IP 地址并更新我从 JSON RESTful API 中提取的数组。我可以很好地将数据从数组中提取出来。这是我的代码到目前为止的样子(请原谅代码的外观)
import requests
import json
import sys
pool_name = sys.argv[1]
add_node = sys.argv[2]
url = 'https://<stingray RESTApi>:9070/api/tm/1.0/config/active/pools/' + pool_name
jsontype = {'content-type': 'application/json'}
client = requests.Session()
client.auth = ('<username>', '<password>')
client.verify = 0
response = client.get(url)
pools = json.loads(response.content)
nodes = pools['properties']['basic']['nodes']
现在我一直在考虑使用这个
client.put(url, <I am stuck>, headers = jsontype)
在这一点上,我已经达到了我目前对 Python 的了解的极限(因为我最近几天才开始学习)。我也研究过使用类似的方法将收集到的数据附加到数组中,然后尝试将其放入。
updatepool['properties']['basic']['nodes'].append(add_node)
当我打印更新池时,我看到我所追求的正在工作,但再次将它放入数组中让我感到难过。
任何帮助将非常感激。
谢谢
更新:这是我的代码的更新,从 API 获得 400 响应
#!/usr/bin/python
import requests
import json
import sys
pool_name = sys.argv[1]
#add_node = sys.argv[2]
add_node = u'10.10.10.1:80'
url = 'https://<uri>:9070/api/tm/1.0/config/active/pools/' + pool_name
jsontype = {'content-type': 'application/json'}
client = requests.Session()
client.auth = ('<username>', '<password')
client.verify = 0
response = client.get(url)
pools = json.loads(response.content)
nodes = pools['properties']['basic']['nodes']
data = nodes
data.append(add_node)
print client.put(url,json.dumps(data), headers=jsontype)