mail_helper.py是邮件操做包,用来发送邮件的。html
1 #!/usr/bin/evn python 2 # coding=utf-8 3 4 import smtplib 5 from email.mime.text import MIMEText 6 from traceback import format_exc 7 from config import const 8 9 # 初始化邮件参数 10 smtp = const.SMTP 11 port = const.PORT 12 user = const.EMAIL_USER 13 passwd = const.EMAIL_PWD 14 email_list = const.EMAIL_LIST 15 err_title = const.EMAIL_ERR_TITLE 16 17 18 def send_mail(subject, context, to_list): 19 ''' 20 发送邮件 21 接收参数: 22 subject 邮件主题 23 context 邮件内容 24 to_list 接收者邮件列表,每一个邮件地址用","分隔 25 ''' 26 if not subject or not context or not to_list: 27 return '邮件发送失败,邮件主题、内容与收件人邮件都是必填项' 28 29 # 初始始化邮件相关参数 30 email = MIMEText(context, 'html', 'utf-8') 31 email['To'] = to_list 32 email['Subject'] = subject 33 email['From'] = user 34 35 # QQ邮箱改成ssl方式发送了 36 # s = smtplib.SMTP(smtp) 37 s = smtplib.SMTP_SSL(smtp) 38 try: 39 s.login(user, passwd) 40 s.sendmail(user, email_list, email.as_string()) 41 s.close() 42 return None 43 except Exception as e: 44 s.close() 45 stacktrace = format_exc() 46 return '邮件发送失败,出现异常:' + str(e.args) + stacktrace + '\n' 47 48 49 def send_error_mail(context): 50 ''' 51 发送邮件 52 接收参数: 53 context 邮件内容 54 ''' 55 if not context: 56 return '邮件内容是必填项' 57 58 send_mail(err_title, context, email_list)
send_mail()函数只须要提交邮件标题、内容和收件人列表,就能够将邮件发送出去,使用的发件人是前面配置const.py里设置的账号,若是没有在配置里设置好对应的帐号密码,邮件将会发送不成功。python
send_error_mail()函数是用来发送异常日志信息的,它默认是给log_helper.py里的异常日志记录函数error()调用,这样当代码执行时出现异常,咱们第一时间就会收到这封异常邮件,而后能够针对性的去进行处理。固然若是服务器出现故障时,有可能会一会儿收到很是多的邮件,被邮件服务器封掉IP的。因此通常我都会用本身的邮箱给本身发,这样万一给封了IP仍是能够收到发送不成功的邮件的。另外,前面说过,api文件夹里的__init__.py和其余文件夹的这个文件有点不同,你们能够对比一下,它会帮咱们解决不少很基本的问题,特别是更新线上代码时,有时会忘记提交新建的python文件,而这个文件又被其它文件所调用,这时python初始化就会发生异常,第一时间咱们就会收到提醒邮件,避免线上服务挂了也不知道的状况发生。api
send_error_mail()函数的邮件标题能够在const.py配置中进行设置(见下面参数),通常我会分开发、测试、预生产、生产等标题,这样在收到邮件时方便咱们区分是那一个环境出现了故障服务器
### 邮件服务参数 ### # 邮件服务器 SMTP = 'smtp.qq.com' # 邮件服务器端口 PORT = 465 # email发送帐号 EMAIL_USER = 'xxxxxx@qq.com' # email发送密码 EMAIL_PWD = 'xxxxxxxxxxxxxxxxx' # 系统异常邮件通知地址,多个地址用逗号分隔 EMAIL_LIST = 'xxxxxx@qq.com' # 异常邮件通知标题 # ——因为咱们有开发环境、测试环境、预生产环境、生产环境等多个不一样的环境, # ——因此在发送异常通知时若是区分的话,可能就弄不清是那个环境出了问题, # ——咱们能够经过设置邮件标题为:开发、测试、预生产、生产等标签来方便区分是那个环境发送的异常通知 EMAIL_ERR_TITLE = '系统异常通知-simple-开发'
测试用例:dom
#!/usr/bin/evn python # coding=utf-8 import unittest from common import mail_helper, except_helper class MailHelperTest(unittest.TestCase): """邮件操做包测试类""" def setUp(self): """初始化测试环境""" print('------ini------') def tearDown(self): """清理测试环境""" print('------clear------') def test(self): mail_helper.send_mail('test', 'test', '1654937@qq.com') except_info = except_helper.detailtrace() mail_helper.send_error_mail('出现异常,堆栈信息:' + except_info) if __name__ == '__main__': unittest.main()
执行结果:ide
log_helper.py是日志操做包函数
1 #!/usr/bin/evn python 2 # coding=utf-8 3 4 import logging 5 import logging.handlers 6 import traceback 7 8 from common import mail_helper, except_helper 9 10 11 def info(content): 12 """记录日志信息""" 13 if content: 14 logging.info(content) 15 16 def error(content = '', is_send_mail = True): 17 """记录错误日志信息""" 18 if traceback: 19 content = content + '\n' + traceback.format_exc() + '\n' 20 # 获取程序当前运行的堆栈信息 21 detailtrace = except_helper.detailtrace() 22 content = content + '程序调用堆栈的日志:' + detailtrace + '\n' 23 24 logging.info(content) 25 26 # 发送邮件通知相关人员 27 if is_send_mail: 28 info = mail_helper.send_error_mail(context=content) 29 if info: logging.info(info)
info()函数用于记录程序执行过程当中的一些信息,好比与第三方接口(最多见的是支付接口)通信时,将提交的网址、参数和返回的结果记录下来,方便咱们在须要时查看,排查出错问题;好比咱们须要排查生产环境异常,定位错误信息位置时,在相关代码中间添加,而后将相关数据变量值记录下来,帮助咱们定位问题所在......测试
error()函数除了拥有info()函数的功能外,它在记录信息的同时,还会自动发送一封邮件到咱们的邮箱。经过它放在try...except...中。ui
#!/usr/bin/evn python # coding=utf-8 import logging import os import unittest from common import log_helper class LogHelperTest(unittest.TestCase): """日志操做包测试类""" def setUp(self): """初始化测试环境""" print('------ini------') # 获取本脚本所在的上级路径(由于log_helper_text.py是在test目录下面,并不在根目录,而咱们想将日志都记录在根据目录下的log目录里,因此须要获取test的上级目录) program_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) # 初始化日志目录 log_path = os.path.join(program_path, 'log') # 当日志目录不存在时建立日志目录 if not os.path.exists(log_path): os.mkdir(log_path) # 定义日志输出格式 logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', filename="%s/info.log" % log_path, filemode='a') def tearDown(self): """清理测试环境""" print('------clear------') def test(self): log_helper.info('记录代码执行的相关记录或信息') try: result = '0' / 10 except Exception as e: log_helper.error('出现异常:' + str(e.args)) if __name__ == '__main__': unittest.main()
执行结果:spa
random_helper.py是随机数操做包,经过里面的函数,咱们能够方便快捷的获取指定大小范围的数值型随机数;指定长度的数字、大小写字母、数字与字母混合型随机数;获取uuid随机码。
1 #!/usr/bin/evn python 2 # coding=utf-8 3 4 import random 5 import uuid 6 from common import encrypt_helper 7 8 ### 定义常量 ### 9 # 小写字母 10 lowercase_letters = "abcdefghijklmnopqrstuvwxyz" 11 # 大写字母 12 majuscule = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 13 # 数字 14 numbers = "0123456789" 15 ################ 16 17 def ___get_randoms(length, text): 18 """ 19 内部函数,获取指定长度的随机字符 20 :param length: 将要生成的字符长度 21 :param text: 生成随机字符的字符池 22 :return: 生成好的随机字符串 23 """ 24 return random.sample(text, length) 25 26 def get_number(length): 27 """ 28 获取指定长度的数字,类型是字符串 29 :param length: 将要生成的字符长度 30 :return: 生成好的随机字符串 31 """ 32 return ''.join(___get_randoms(length, numbers)) 33 34 def get_number_for_range(small, max): 35 """ 36 获取指定大小的整形数值 37 :param small: 最小数值 38 :param max: 最大数值 39 :return: 生成好的随机数值 40 """ 41 return random.randint(small, max) 42 43 def get_string(length): 44 """ 45 获取指定长度的字符串(大小写英文字母+数字) 46 :param length: 将要生成的字符长度 47 :return: 生成好的随机字符串 48 """ 49 return ''.join(___get_randoms(length, lowercase_letters + majuscule + numbers)) 50 51 def get_letters(length): 52 """ 53 生成随机英文字母字符串(大小写英文字母) 54 :param length: 将要生成的字符长度 55 :return: 生成好的随机字符串 56 """ 57 return ''.join(___get_randoms(length, lowercase_letters + majuscule)) 58 59 def get_uuid(): 60 """ 61 随机生成uuid 62 :return: 生成好的uuid 63 """ 64 return str(uuid.uuid4()).replace('-', '')
由于比较简单,因此不一一说明,直接看测试用例
#!/usr/bin/evn python # coding=utf-8 import unittest from common import random_helper class RandomHelperTest(unittest.TestCase): """随机数操做包测试类""" def setUp(self): """初始化测试环境""" print('------ini------') def tearDown(self): """清理测试环境""" print('------clear------') def test(self): print('获取0到100之间的随机数') print(random_helper.get_number_for_range(0, 100)) print(random_helper.get_number_for_range(0, 100)) print('获取长度为5的数字随机码') print(random_helper.get_number(5)) print(random_helper.get_number(5)) print('获取长度为6的英文随机码') print(random_helper.get_letters(6)) print(random_helper.get_letters(6)) print('获取长度为6的数字与英文随机码') print(random_helper.get_string(6)) print(random_helper.get_string(6)) print('获取uuid') print(random_helper.get_uuid()) print(random_helper.get_uuid()) if __name__ == '__main__': unittest.main()
执行结果:
------ini------ 获取0到100之间的随机数 54 21 获取长度为5的数字随机码 20156 58132 获取长度为6的英文随机码 BqQCZP ybFIaB 获取长度为6的数字与英文随机码 FZfEgd GAslRy 获取uuid 2aba0e946414434ea6b7f2e425d8b41b 52fe4545b09443a088ce460453d909fa ------clear------
版权声明:本文原创发表于 博客园,做者为 AllEmpty 本文欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然视为侵权。
python开发QQ群:669058475(本群已满)、733466321(能够加2群) 做者博客:http://www.cnblogs.com/EmptyFS/