是不是還在苦惱服務(wù)掛了蚁趁,不能先人一步据途?“老衲已經(jīng)知道服務(wù)異常了,待我稍加處理朱巨∈非蹋”此時投來無數(shù)敬佩的目光(吃瓜群眾) *——*
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"] #接收信息的郵箱地址,可以多多個 “冀续,”區(qū)別
mail_host="smtp.qq.com"? #設(shè)置服務(wù)器
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"? #發(fā)件箱的后綴
def send_mail(to_list,sub,content):? #to_list:收件人琼讽;sub:主題;content:郵件內(nèi)容
me="業(yè)務(wù)平臺系統(tǒng)異常"+"<"+mail_user+"@"+mail_postfix+">"? #這里的hello可以任意設(shè)置洪唐,收到信后钻蹬,將按照設(shè)置顯示
msg = MIMEText(content)? ? #創(chuàng)建一個實例,這里設(shè)置為html格式郵件
msg['Subject'] = sub? ? #設(shè)置主題
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP_SSL(mail_host, 465)? #smtplib發(fā)送帶SSL認證的郵箱系統(tǒng)
s.set_debuglevel(1)
s.login(mail_user,mail_pass)? #登陸服務(wù)器
s.sendmail(me, to_list, msg.as_string())? #發(fā)送郵件
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,"業(yè)務(wù)平臺端口檢測失斊拘琛:10086","error")
time.sleep(600)