python 邮件通知程序
短信预约 -IT技能 免费直播动态提醒
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys)
from email.MIMEText import MIMEText
import smtplib
sys.setdefaultencoding('utf-8')
import socket, fcntl, struct
def send_mail(to_list,sub,content):
mail_host="smtp.163.com"
mail_user="XXXXX@163.com"
mail_pass="XXXXXXX"
mail_postfix="163.com"
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
def get_local_ip(ifname = 'eth0'):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
ret = socket.inet_ntoa(inet[20:24])
return ret
if sys.argv[1]!="master" and sys.argv[1]!="backup" and sys.argv[1]!="fault":
sys.exit()
else:
notify_type = sys.argv[1]
if __name__ == '__main__':
strcontent = get_local_ip()+ " " +notify_type+" State is activated, please make sure the HAProxy service running state!"
mailto_list = ['XXXXX@139.com, ]
for mailto in mailto_list:
send_mail(mailto, "HAProxy State switch alarm", strcontent.encode('utf-8'))
参考南非蚂蚁《高性能Linux服务器构建实战》
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341