代码改变世界

python 根据监控结果发送邮件

2012-08-13 10:04  java环境变量  阅读(360)  评论(0编辑  收藏  举报

使用gg后,需要监控,自己写个自动发送邮件,这只是个原型!根据从数据库返回的数据结果是1还是2来判断同步情况,

并及时发送邮件并短信通知以及记录日志!

 


# -*- coding: gb2312 -*-

import re  
import  pyodbc
import traceback
import decimal
import os, sys
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '1364188****@139.com'
receiver = '1364188****@139.com'
subject = '数据同步监控'
smtpserver = 'smtp.139.com'
username = '1364188****'
password = '************'


########判断同步是否正常

guess = int(input("请输入一个整数:\n"))
if guess == 1:
     msg = MIMEText('监控正常!','plain','gb2312')#中文需参数‘gb2312’,单字节字符不需要
     msg['Subject'] = Header(subject, 'gb2312')
     msg['date']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
     smtp = smtplib.SMTP()
     smtp.connect('smtp.139.com')
     smtp.login(username, password)
     smtp.sendmail(sender, receiver, msg.as_string())
     logtxt='监控邮件已经在'+msg['date']+'发送成功!'+':同步正常'
     smtp.quit()
     print (logtxt)
     f = open('e:\py\log.txt', 'a')
     f.write(logtxt+'\n')
     f.close()
     print ( '日志:'+logtxt+'记录成功' )
elif guess == 2:
     msg = MIMEText('监控异常!','plain','gb2312')#中文需参数‘gb2312’,单字节字符不需要
     msg['Subject'] = Header(subject, 'gb2312')
     msg['date']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
     smtp = smtplib.SMTP()
     smtp.connect('smtp.139.com')
     smtp.login(username, password)
     smtp.sendmail(sender, receiver, msg.as_string())
     logtxt='监控邮件已经在'+msg['date']+'发送成功!'+':同步异常'
     smtp.quit()
     print (logtxt)
     f = open('e:\py\log.txt', 'a')
     f.write(logtxt+'\n')
     f.close()
     print ( '日志:'+logtxt+'记录成功' )
   

else:

    print ('**************输入类型不对,请联系ocpyang!**********************')