异步协程 aiohttp 实现企业微信消息推送报警


异步协程 


aiohttp  注意语法糖 (async \await )异步调运 


关键词


event_loop 事件循环:程序开启一个无限的循环,程序员会把一些函数注册到事件循环上。当满足事件发生的时候,调用相应的协程函数。


coroutine 协程:协程对象,指一个使用async关键字定义的函数,它的调用不会立即执行函数,而是会返回一个协程对象。协程对象需要注册到事件循环,由事件循环调用。


task  任务:一个协程对象就是一个原生可以挂起的函数,任务则是对协程进一步封装,其中包含任务的各种状态。


future: 代表将来执行或没有执行的任务的结果。它和task上没有本质的区别


async/await 关键字:python3.5 用于定义协程的关键字,async定义一个协程,await用于挂起阻塞的异步调用接口。


 






1
# !/usr/bin/python3 2 3 # -*- coding: utf-8 -*- 4 5 # Author: WangChao 6 7 import asyncio 8 9 import json 10 11 import logging 12 13 import time 14 15 from aiohttp import ClientSession 16 17 class Aiowechat: 18 19 options= None 20 21 def __init__(self, options=None): 22 23 self.options= optionsif isinstance(options,dict) else dict() 24 25 self.corpid= '**********' 26 27 self.corpsecret= '***********' 28 29 self.url= '************' 30 31 self.token_now= 0 32 33 self.token= None 34 35 @staticmethod 36 37 async def load_logger(): 38 39 logger= logging.getLogger(name=__name__) 40 41 return logger 42 43 async def get_token(self): 44 45 """获取token值""" 46 47 if int(time.time()) - self.token_now> 7000: 48 49 token_url= '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (self.url,self.corpid,self.corpsecret) 50 51 async with ClientSession() as session: 52 53 async with session.get(token_url) as response: 54 55 content= await response.read() 56 57 token= json.loads(content.decode()).get('access_token','') 58 59 self.token= token 60 61 self.token_now= int(time.time()) 62 63 return self.token 64 65 @staticmethod 66 67 async def messages(touser, wechat_msg): 68 69 """构建发送数据""" 70 71 values= { 72 73 "touser": touser, 74 75 "msgtype": "text", 76 77 "agentid": 1000007, 78 79 "text": { 80 81 "content": wechat_msg,}, 82 83 "safe": 0, 84 85 "enable_id_trans": 0, 86 87 "enable_duplicate_check": 0, 88 89 } 90 91 msges= (bytes(json.dumps(values),'utf-8')) 92 93 return msges 94 95 async def send(self, touser, wechat_data): 96 97 """发送""" 98 99 token= await self.get_token() 100 101 msg= wechat_data.get('msg') 102 103 send_url= '%s/cgi-bin/message/send?access_token=%s' % (self.url, token) 104 105 data= await self.messages(touser, msg) 106 107 async with ClientSession() as session: 108 109 async with session.post(send_url,data=data) as response: 110 111 response= await response.read() 112 113 state= json.loads(response.decode()).get('errcode','') 114 115 logger= await self.load_logger() 116 117 if state== 0: 118 119 logger.warning('企业微信报警通知 to-{} 成功'.format(touser)) 120 121 else: 122 123 logger.error('企业微信报警通知 to-{0}失败 状态码:{1}'.format(touser, state)) 124 125 async def exec(self, data): 126 127 tousers= data.get('tousers') 128 129 tasks= [] 130 131 for touserin tousers: 132 133 tasks.append(asyncio.ensure_future(self.send(touser, data))) 134 135 return tasks 136 137 if __name__== '__main__': 138 139 we= Aiowechat(Aiowechat.load_env()) 140 141 data= { 142 143 'tousers': ['***','***********'], 144 145 'msg': '多人测试', 146 147 } 148 149 loop= asyncio.get_event_loop() 150 151 tasks= loop.run_until_complete(we.exec(data=data)) 152 153 loop.run_until_complete(asyncio.wait(tasks))

 

posted @ 2020-03-06 15:28  iotwolves  阅读(291)  评论(0编辑  收藏  举报