# user.login方法获取zabbix server的认证结果
#!/usr/bin/env python2.7
#coding=utf-8
import json
import urllib.request
import urllib.error
# based url and required header
url = "http://192.168.29.130/zabbix/api_jsonrpc.php"
header = {"Content-Type":"application/json"}
# auth user and password
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 0
}
)
data = bytes(data, 'utf8')
# create request object
request = urllib.request.Request(url,data)
for key in header:
request.add_header(key,header[key])
# auth and get authid
try:
result = urllib.request.urlopen(request)
res = result.read().decode('utf-8')
except urllib.error.URLError as e:
print ("Auth Failed, Please Check Your Name AndPassword:",e.code)
else:
response = json.loads(res)
result.close()
print("Auth Successful. The Auth ID Is:",response['result'])
转载于:https://www.cnblogs.com/zzwhjj/p/5664254.html