把traceback信息传递给字符串,并且发送给邮箱,traceback信息传递,很简单的代码主要用到tr
把traceback信息传递给字符串,并且发送给邮箱,traceback信息传递,很简单的代码主要用到tr
很简单的代码主要用到traceback的file参数imspect模块主要用于输出错误文件路径,方便查看错误出处应该需要配置 邮件发送服务器
[Python]代码
import osimport sysimport tracebackimport inspectimport datetimeimport smtplib, mimetypesfrom email.MIMEText import MIMETextclass MailSender(object): ''' 向邮箱发送错误信息 用法:MailSender().SendMail(receiverlist, sub = 'AntiFraud ERROR', content= 'Error info') ''' def __init__(self): self.frm = "******@***.***" self.message = '' # restore the info of traceback self.errFilePath = '' def write(self, str): ''' 把traceback信息存储必须的函数 ''' self.message += str def __getContent(self): ''' 得到traceback信息 ''' traceback.print_exc(file = self) def __getReceiverList(self, receivers) : ''' 得到收件人列表 ''' if isinstance(receivers, list): return ';'.join(receivers) return receivers def __getErrFilePath(self): ''' 得到发生error的文件的路径 ''' n = len(inspect.stack()) current_file = inspect.stack()[n-1][1] return os.path.abspath(current_file) def sendMail(self, receiver, sub = 'ERROR sub', content= 'Error info'): ''' 发送邮件 ''' receiverlist = self.__getReceiverList(receiver) self.__getContent() self.errFilePath = self.__getErrFilePath() content = ">>>%s<<<%s%s%s%s" % (content, '\nin: ',str(self.errFilePath), '\n', self.message) try: msg = MIMEText(content) msg['From'] = self.frm msg['To'] = receiverlist msg['Subject'] = sub smtp_server = smtplib.SMTP('localhost') smtp_server.sendmail(self.frm, receiver, msg.as_string()) smtp_server.quit() except Exception, ex: print 'Error when sending email' raise ex #def test(): receiver = ['*****@126.com'] mm = MailSender() try: a = int('') except Exception, ex: #sub = 'Error' #content = 'ERror when program running' mm.sendMail(receiver)if __name__ =='__main__': test()
相关内容
- python 生成java执行的shell文件,pythonshell,我有这么一个
- python自动投票测试,python投票测试,python伪造HTTP
- python Gevent multiprocessing server,,import sysfr
- python使用ssh连接远程服务器,并执行命令代码,python
- python 使用poplib库pop3协议收邮件代码,pythonpoplib,# -*-
- python使用PIL制作水印,pythonpil水印,利用python的PIL
- python生成验证码(webpy完整验证码类),pythonwebpy,python生成
- python小数除法取整数部分,python小数除法整数,python中的
- python获得url的顶级域名,pythonurl顶级域名,如下代码to
- python使用ctypes模块调用windowsapi获取系统版本,ctypeswi
评论关闭