zabbix-API-自定义库

15 篇文章 0 订阅
6 篇文章 0 订阅
#!/usr/bin/python
# encoding: utf-8
#filename: zabbix_base_api.py
#author: gaohaixiang
#writetime:20190713

import json
# import urllib.request
import urllib2

class zabbix_base_api(object):

    def __init__(self,url = "http://192.168.73.9/api_jsonrpc.php" ,header = {"Content-Type": "application/json"}):
        self.url = url
        self.header = header
    # post
    def post_request(self,url, data, header):
        request = urllib2.Request(url, data, header)    #python2使用
        result = urllib2.urlopen(request)
#
        # request = urllib.request.Request(url, data, header)   #python3使用
        # result = urllib.request.urlopen(request)
    #    ccc = result.read()
    #   print (ccc)
    #    response = json.loads(ccc)
        response = json.loads(result.read())
        result.close()
        return response

    def json_data(self,method,params,authid):
        data = json.dumps(
        {
            "jsonrpc": "2.0",
            "method": method,
            "params": params,
            "auth": authid,
             "id": 1
        })
        request_info = self.post_request(self.url, data.encode('utf-8'), self.header)

        return request_info
    # 认证信息
    def authid(self,user,password): #定json中auth的信息
        data = json.dumps(
        {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": user,
                "password": password
            },
            "id": 1
        })
        authid = self.post_request(self.url, data.encode('utf-8'), self.header)
        try:
            return authid['result']
        except KeyError:
            print ('用户名或密码错误')
            exit()

    def text_process(self,file):
        import re
        find = re.compile(r"^#")
        text_info = []
        # f =  open(file,"r",encoding='UTF-8')
        f = open(file, "r")
        text = f.readlines()
        f.close()
        for i in text:
            if find.search(i.rstrip("\n")) == None:
                text_info.append(i.rstrip("\n"))
        return text_info

    # 注销认证
    def login_out(self,authid):
        data = json.dumps(
        {
            "jsonrpc": "2.0",
            "method": "user.logout",
            "params": [],
            "id": 1,
            "auth": authid
        })
        a = self.post_request(self.url, data.encode('utf-8'), self.header)
        return '认证信息已注销'


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值