#!/usr/bin/python
import urllib2
import json
def login(username,password,url,data):
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
try:
result = urllib2.urlopen(request)
except URLError as e:
print "url error: ", e.code
else:
response = json.loads(result.read())
result.close()
print "login is OK your id:",response['result']
if __name__ == '__main__':
username = 'zabbix用户名'
password = 'zabbix密码'
url = 'http://你的zabbix服务器地址 /api_jsonrpc.php'
header = {"Content-Type":"application/json"}
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":username,
"password":password
},
"id":0
})
login(username,password,url,data)
转载于:https://blog.51cto.com/xiaofengmo/1747336