#!/usr/bin/env python #coding=utf-8 import json import ast import smtplib from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkons.request.v20190214.OnsTopicListRequest import OnsTopicListRequest from email.mime.text import MIMEText from email.header import Header class ali_conan_mq: get_ali_conan_diff_list = [] get_ali_dev_topic_list = [] get_ali_online_topic_list = [] Dev_InstanceId = "xxx" On_InstanceId = "xxxx" def __init__(self,**kwargs): self.AcskeyId = "xxxx" self.AcskeySercet = "xxxx" self.Regin = "cn-beijing" # 获取aliyun conan_dev topic def get_conan_dev_rabbitmq(self): client = AcsClient(self.AcskeyId,self.AcskeySercet,self.Regin) request = OnsTopicListRequest() request.set_accept_format('json') request.set_InstanceId(ali_conan_mq.Dev_InstanceId) response = client.do_action_with_exception(request) data=json.loads(response) for list in data['Data']['PublishInfoDo']: ali_conan_mq.get_ali_dev_topic_list.append(list['Topic']) # print ali_conan_mq.get_ali_dev_topic_list def get_conan_online_rabbitmq(self): client = AcsClient(self.AcskeyId, self.AcskeySercet, self.Regin) request = OnsTopicListRequest() request.set_accept_format('json') request.set_InstanceId(ali_conan_mq.On_InstanceId) response = client.do_action_with_exception(request) data=json.loads(response) for list in data['Data']['PublishInfoDo']: ali_conan_mq.get_ali_online_topic_list.append(list['Topic']) def diffit_conan_online_rabbitmq(self): self.get_conan_dev_rabbitmq() self.get_conan_online_rabbitmq() for new_list in ali_conan_mq.get_ali_online_topic_list: if new_list not in ali_conan_mq.get_ali_dev_topic_list: ali_conan_mq.get_ali_conan_diff_list.append(new_list) return ali_conan_mq.get_ali_conan_diff_list class YFD_Email(): SMTP_SERVER = "smtp.163.com" SMTP_PORT = 25 SMTP_USERNAME = "xxxxxxxx@163.com" SMTP_PASSWORD = "xxxxx" EMAIL_TO = ["xxxxx@fenbi.com"] EMAIL_FROM = "xxxxxx@163.com" EMAIL_SUBJECT = "测试发送rabbit-topic-diff" EMAIL_SPACE = ", " def task_list(self): rabbit = ali_conan_mq() topic_value = rabbit.diffit_conan_online_rabbitmq() with open('alimq_list.txt', 'w+') as f: for name in topic_value: f.write('%s ' % name) def send_mail(self): self.task_list() with open('alimq_list.txt','r') as f1_top_list: data1=f1_top_list.read() msg = MIMEText('您的阿里云实例线上服务:conan-online 和阿里里云实例测试服务器:conan-test 对比产生的topic列表情况 如下(以下topic 为线上环境有而测试环境没有的topic) %s' %(data1)) # msg['Subject'] = self.EMAIL_SUBJECT + " %s" % (date.today().strftime(self.DATE_FORMAT)) msg['Subject'] = self.EMAIL_SUBJECT msg['To'] = self.EMAIL_SPACE.join(self.EMAIL_TO) msg['From'] = self.EMAIL_FROM mail = smtplib.SMTP(self.SMTP_SERVER, self.SMTP_PORT) mail.starttls() mail.login(self.SMTP_USERNAME, self.SMTP_PASSWORD) mail.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string()) mail.quit() if __name__ == '__main__': # YFD_ali_mq = ali_conan_mq() # YFD_ali_mq.diffit_conan_online_rabbitmq() Y_email = YFD_Email() Y_email.send_mail()