当我们在用zabbix api做开发时,需要获取zabbix api token,本文简单介绍token的获取方法
用urllib2 给user.login接口post url,以及登录参数,从返回结果中获取token
代码如下:
#/usr/bin/python env
#coding:utf8
#-*- coding: utf-8 -*-
class Authenticate(object):
def __init__(self):
self.url = 'http://192.168.100.20:1080/api_jsonrpc.php'
self.username = 'admin'
self.password = 'zabbix'
def requestJson(self,values):
import urllib2
import json
data = json.dumps(values)
req = urllib2.Request(self.url,data,{'Content-Type': 'application/json-rpc'})
response = urllib2.urlopen(req,data)
output = json.loads(response.read())
try:
message = output['result']
except:
message = output['error']['data']
return message
@property
def token(self):
values = {"jsonrpc": '2.0',"id": 1}
values['method'] = 'user.login'
values['params'] = {'user': self.username,'password': self.password}
token = self.requestJson(values)
return token
网友评论