Import time
Import requests
Import Smtplib
From Email.mime.text import Mimetext
Import datetime
def SendEmail (Fromadd,toadd,subject,text):
_pwd = "[email protected]" #授权码
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_= "[Email protected]"
To_ = ["[Email protected]"] #收件人地址
Subject = "URL Alarm information"
# 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" alerts with a value of:%d Please check the status "% (NOWTIME,I,V) in time
For I in To_:
SendEmail (From_, I, subject, text)
Except Requests.exceptions.ConnectionError as E:
Text = "%s%s alert, service may hang out if SSL certificate possible certificate expires"% (nowtime,i)
For I in To_:
SendEmail (From_, I, subject, text)
Python URL monitoring and mail alerting