是不是还在苦恼服务挂了,不能先人一步?“老衲已经知道服务异常了,待我稍加处理。”此时投来无数敬佩的目光(吃瓜群众) *——*
Code
#-*- encoding:utf-8 -*-
#!/usr/bin/env python
import os
import socket
import time
import smtplib
from email.mime.text import MIMEText
mailto_list=["XXXXXX@qq.com"] #接收信息的邮箱地址,可以多多个 “,”区别
mail_host="smtp.qq.com" #设置服务器
mail_user="xxxxxx" #用户名
mail_pass="xxxxx" #口令获取方式 QQ http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
mail_postfix="qq.com" #发件箱的后缀
def send_mail(to_list,sub,content): #to_list:收件人;sub:主题;content:邮件内容
me="业务平台系统异常"+"<"+mail_user+"@"+mail_postfix+">" #这里的hello可以任意设置,收到信后,将按照设置显示
msg = MIMEText(content) #创建一个实例,这里设置为html格式邮件
msg['Subject'] = sub #设置主题
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP_SSL(mail_host, 465) #smtplib发送带SSL认证的邮箱系统
s.set_debuglevel(1)
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 PortCheck(ip,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((ip,int(port)))
s.shutdown(2)
print '%d is open' % port
return True
except:
print '%d is down' % port
return False
if __name__ == '__main__':
while(1>0):
flag=1
flag=PortCheck('127.0.0.1',10086)
print flag
time.sleep(10)
if flag==False:
send_mail(mailto_list,"业务平台端口检测失败:10086","error")
time.sleep(600)