Python URL monitoring and mail alerting

Source: Internet
Author: User
Tags ssl certificate

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.