码迷,mamicode.com
首页 > 编程语言 > 详细

python url监控 并邮件报警

时间:2018-08-10 15:47:09      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:erro   邮件   监控   mail   sendemail   stl   ESS   subject   str   

import time
import requests
import smtplib
from email.mime.text import MIMEText
import datetime

def SendEmail(fromAdd,toAdd,subject,text):

_pwd = "Yeer2016@sudai" #授权码

msg = MIMEText(text)
msg["Subject"] = subject
msg["From"] = fromAdd
msg["To"] = toAdd
try:
s = smtplib.SMTP_SSL("smtp.sudaizhijia.com", 465)
s.login(fromAdd, _pwd)
s.sendmail(fromAdd, toAdd, msg.as_string())
s.quit()
print ("Success!")
except smtplib.SMTPException:
print(‘Falied!‘)
from_="tech-report@sudaizhijia.com"
to_ = ["wangdong@sudaizhijia.com"] #收件人地址
subject = "URL报警信息"
# url_list=["http://api.sudaizhijia.com","http://event.fastloan360.com","http://www.qianzhouzhou.com","https://data.sudaizhijia.com","http://uat.data.sudaizhijia.com"] #url地址
url_list=["https://m.1sudai.com/","http://www.qianzhouzhou.com"]


while True:
time.sleep(20)

for i in url_list:
nowTime = datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
try:
code = requests.get(i).status_code
v = int(code)
time.sleep(2)
if isinstance(v,int):
if v == 200:
pass
else:
text = "%s %s 发出报警,值为:%d 请及时查看状态" %(nowTime,i,v)
for i in to_:
SendEmail(from_, i, subject, text)

except requests.exceptions.ConnectionError as e:
text = "%s %s 发出报警,服务可能挂掉 如果有ssl证书可能证书到期" %(nowTime,i)
for i in to_:
SendEmail(from_, i, subject, text)

python url监控 并邮件报警

标签:erro   邮件   监控   mail   sendemail   stl   ESS   subject   str   

原文地址:https://www.cnblogs.com/wangruwei/p/9454974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!