python使用logging模块发送邮件代码示例


Posted in Python onJanuary 18, 2018

logging模块不只是能记录log,还能发送邮件,使用起来非常简单方便

#coding=utf-8 
''''' 
Created on 2016-3-21 
 
@author: Administrator 
''' 
import logging, logging.handlers 
class EncodingFormatter(logging.Formatter): 
 def __init__(self, fmt, datefmt=None, encoding=None): 
  logging.Formatter.__init__(self, fmt, datefmt) 
  self.encoding = encoding 
 def format(self, record): 
  result = logging.Formatter.format(self, record) 
  if isinstance(result, unicode): 
   result = result.encode(self.encoding or 'utf-8') 
    
  return result 
 
#zhangdongsheng@itouzi.com 
errlog = logging.getLogger() 
sh = logging.handlers.SMTPHandler("smtp.163.com", 'xigongda200608@163.com', '381084992@qq.com', 
    "logging from my app", 
    credentials=('xigongda200608', 'password'), 
    secure=()) 
errlog.addHandler(sh) 
sh.setFormatter(EncodingFormatter('%(message)s', encoding='utf-8')) 
errlog.error(u'追加文件时出错')

总结

以上就是本文关于python使用logging模块发送邮件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python实现根据月份和日期得到星座的方法
Mar 27 Python
基于python元祖与字典与集合的粗浅认识
Aug 23 Python
itchat和matplotlib的结合使用爬取微信信息的实例
Aug 25 Python
为什么选择python编程语言入门黑客攻防 给你几个理由!
Feb 02 Python
Python如何优雅获取本机IP方法
Nov 10 Python
python3正则模块re的使用方法详解
Feb 11 Python
Tensorflow中的dropout的使用方法
Mar 13 Python
Django 后台带有字典的列表数据与页面js交互实例
Apr 03 Python
Vs Code中8个好用的python 扩展插件
Oct 12 Python
Python paramiko使用方法代码汇总
Nov 20 Python
Python实现王者荣耀自动刷金币的完整步骤
Jan 22 Python
opencv实现图像平移效果
Mar 24 Python
zookeeper python接口实例详解
Jan 18 #Python
Python获取当前函数名称方法实例分享
Jan 18 #Python
Python AES加密实例解析
Jan 18 #Python
快速了解python leveldb
Jan 18 #Python
Python实现动态图解析、合成与倒放
Jan 18 #Python
Python基于matplotlib实现绘制三维图形功能示例
Jan 18 #Python
Python实现在tkinter中使用matplotlib绘制图形的方法示例
Jan 18 #Python
You might like
php编程实现获取excel文档内容的代码实例
2011/06/28 PHP
php获取文件夹路径内的图片以及分页显示示例
2014/03/11 PHP
php实现MySQL数据库备份与还原类实例
2014/12/09 PHP
Jquery Change与bind事件代码
2011/09/29 Javascript
12款经典的白富美型—jquery图片轮播插件—前端开发必备
2013/01/08 Javascript
jquery文字上下滚动的实现方法
2013/03/22 Javascript
『jQuery』名称冲突使用noConflict方法解决
2013/04/22 Javascript
js怎么判断flash swf文件是否加载完毕
2014/08/14 Javascript
JS实现单行文字不间断向上滚动的方法
2015/01/29 Javascript
javascript三元运算符用法实例
2015/04/16 Javascript
点击页面任何位置隐藏div的实现方法
2016/09/05 Javascript
基于SpringMVC+Bootstrap+DataTables实现表格服务端分页、模糊查询
2016/10/30 Javascript
微信小程序 WebSocket详解及应用
2017/01/21 Javascript
在javascript中,null>=0 为真,null==0却为假,null的值详解
2017/02/22 Javascript
Vue原理剖析 实现双向绑定MVVM
2017/05/03 Javascript
webpack中的热刷新与热加载的区别
2018/04/09 Javascript
vue实现的下拉框功能示例
2019/01/29 Javascript
在node中使用jwt签发与验证token的方法
2019/04/03 Javascript
[18:20]DOTA2 HEROS教学视频教你分分钟做大人-昆卡
2014/06/11 DOTA
Python实现类的创建与使用方法示例
2017/07/25 Python
python删除某个字符
2018/03/19 Python
python实现验证码识别功能
2018/06/07 Python
20行python代码实现人脸识别
2019/05/05 Python
python+jinja2实现接口数据批量生成工具
2019/08/28 Python
哪种Python框架适合你?简单介绍几种主流Python框架
2020/08/04 Python
Python3合并两个有序数组代码实例
2020/08/11 Python
Hotels.com中国区:好订网
2016/08/18 全球购物
Alexandre Birman美国官网:亚历山大·伯曼
2019/10/30 全球购物
兰蔻法国官方网站:Lancôme法国
2020/02/22 全球购物
简述索引存取方法的作用和建立索引的原则
2013/03/26 面试题
Linux内核产生并发的原因
2012/07/13 面试题
学术会议欢迎词
2014/01/09 职场文书
大学生的自我鉴定范文
2014/01/21 职场文书
职务说明书范文
2014/05/07 职场文书
先进集体事迹材料范文
2014/12/25 职场文书
golang判断key是否在map中的代码
2021/04/24 Golang