See python url monitor the state of e-mail alerts

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

def mail_sent( str ) :
    _user = "[email protected]"
    _pwd = "************"   #自己的授权码
    _to = "[email protected]"

    # 使用MIMEText构造符合smtp协议的header及body
    msg = MIMEText("告警:接口故障"+str)
    msg["Subject"] = "don't panic"
    msg["From"] = _user
    msg["To"] = _to

    s = smtplib.SMTP("smtp.qq.com", timeout=30)
    s.login(_user, _pwd)  # 登陆服务器
    s.sendmail(_user, _to, msg.as_string())  # 发送邮件

#mail_sent('aaa')


def main():
    url_file = open("test.txt")
    for url in url_file.readlines():
        url = url.strip().replace(" ", "").replace("\n", "").replace("\r\n", "")
        status=requests.get(url)
        code = status.status_code
        print(code)
        if code==200:
            mail_sent(f'\n网页返回码:{code}\n接口地址:{url}')

main()
Released nine original articles · won praise 2 · Views 1989

Guess you like

Origin blog.csdn.net/u014692704/article/details/104064355