概述
#!/usr/bin/env python # -*- coding: utf-8 -*- import time import requests import json import os import traceback import logging class WeChat: def __init__(self): self.CORPID = 'ww750dfdfde43613c3' #企业ID,在管理后台获取 self.CORPSECRET = 'rCRzAXI-r7KUZ6uL5HZYYLaqQw2volmnhQrdStu6Qrk'#自建应用的Secret,每个自建应用里都有单独的secret self.AGENTID = '1000011' #应用ID,在后台应用中获取 self.TOUSER = "g1702543|g1702076|g1701677" # 接收者用户名,多个用户用|分割 def _get_access_token(self): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' values = {'corpid': self.CORPID, 'corpsecret': self.CORPSECRET, } req = requests.post(url, params=values,proxies={'https':'https://10.191.131.20:3128'}) ##这次外网访问是通过代理访问的,所以需要proxies指向代理地址 data = json.loads(req.text) return data["access_token"] def get_access_token(self): try: with open('access_token.conf', 'r') as f: t, access_token = f.read().split() except: with open('access_token.conf', 'w') as f: access_token = self._get_access_token() cur_time = time.time() f.write('t'.join([str(cur_time), access_token])) return access_token else: cur_time = time.time() if 0 < cur_time - float(t) < 7260: return access_token else: with open('access_token.conf', 'w') as f: access_token = self._get_access_token() f.write('t'.join([str(cur_time), access_token])) return access_token def send_data(self, message): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token() send_values = { "touser": self.TOUSER, "msgtype": "text", "agentid": self.AGENTID, "text": { "content": message }, "safe": "0" } logging.basicConfig(filename='log.txt', level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s') try: send_msges=(bytes(json.dumps(send_values))) #respone = requests.post(send_url, send_msges) respone = requests.post(send_url, send_msges,proxies={'https':'https://10.191.131.20:3128'}) #respone = requests.post(send_url, send_msges,proxies={'https':'https://10.146.3.177:3128'}) respone = respone.json() #当返回的数据是json串的时候直接用.json即可将respone转换成字典 return respone["errmsg"] except: logging.debug(traceback.format_exc()) if __name__ == '__main__': wx = WeChat() msg="""服务器10.134.167.126 EVMI JOBS计划任务异常告警提示如下: TaskName名称 上次执行失败时间 """ file= open('reulst_text.txt','r') while True: ##循环读取文件内容 line= file.read() msg += line.strip()+'n' ##读取每一行的内容之后,换行在换行读取 if not line: break ##遇到读取空行的时候,结束读取文件 wx.send_data(msg)
异常现象:此处使用了一个代理访问外网,之前此脚本无法通过计划任务发送出去,手动执行可以发送,原因是本地访问外网是通过代理访问的,解决方法是程序需要指向代理访问外网,一直导致请求失败,出现如下异常日志输出:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='qyapi.weixin.qq.com', port=443): Max retries exceeded with url: /cgi-bin/message/send?access_token=9Uyg6rhaThMhcG0r256-KbrJqXr3j8M25kGXafcx7_GPJPMc8Q2fKd9hB4swwICuNTjt-z-fr4xMs0BJE4xTLsfK6JpGxjQegVHM11JgUNRrozVe6SwEcZVNywGHicYmjDzZpotA3Ksvn4Vx9S6F61RP1eQcBoA8vFyhIHxD5iThuJcPTzFzZd34HT37AGdcwtmQYGlg3q73WSQYgoHARg (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fd808dcc850>: Failed to establish a new connection: [Errno 111] Connection refused',))
转载于:https://www.cnblogs.com/NGU-PX/p/11321937.html
最后
以上就是轻松夕阳为你收集整理的企业微信公众号告警Python脚本的全部内容,希望文章能够帮你解决企业微信公众号告警Python脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复