import requests import json import os # user config here ip = '192.168.52.130' user = "root" password = "123" # user config end class ZabbixApi: def __init__(self, ip, user, password): self.url = 'http://' + ip + '/api_jsonrpc.php' self.headers = {"Content-Type": "application/json", } self.user = user self.password = password self.auth = self.__login() def __login(self): ''' zabbix登陆获取auth :return: auth # 样例bdc64373690ab9660982e0bafe1967dd ''' data = json.dumps({ "jsonrpc": "2.0", "method": "user.login", "params": { "user": self.user, "password": self.password }, "id": 1, }) try: response = requests.post(url=self.url, headers=self.headers, data=data, timeout=2) # {"jsonrpc":"2.0","result":"bdc64373690ab9660982e0bafe1967dd","id":10} auth = response.json().get('result', '') return auth except Exception as e: print("