此脚本根据检测网站是否有设定的“关键字”来判断网站是否异常,如果设定服务器每分钟运行脚本,当网站访问异常时,邮箱每分钟都会发送邮件,更好的通知方式待以后技术提升再优化。
password的xxx为自己的邮箱密码,如果用163或腾讯等邮箱时,需要申请授权码当作密码使用。
#!/usr/bin/python3
def re(url):
import requests
x = requests.get(url)
txt = x.text
#b = txt.find('直播')
return txt
#用户请求测试的网站,得到返回字符串txt
def ya(a):
import yagmail
mail = yagmail.SMTP(user='qtorm1@163.com', host='smtp.163.com', password='xxx')
contents = [a,'网站已挂']
mail.send(to=['yanjing@gzjstd.cn'], subject= '网站访问异常', contents=contents)
mail.close()
#访问异常发送的邮件
def ya1(a):
import yagmail
mail = yagmail.SMTP(user='qtorm1@163.com', host='smtp.163.com', password='xxx')
contents = [a,'能正常访问']
mail.send(to=['yanjing@gzjstd.cn'], subject='%s网站访问正常' % a, contents=contents)
mail.close()
#用于测试访问正常发送的邮件
def test(name,url,keyword):
b = re(url)
b = b.find(keyword)
if b < 0:
ya(name)
#uname:项目名,usl:访问链接,keyword:关键字,关键字判断
test('猎趣','https://liequ.njcywl88.com/','直播')
test('火力','http://www.huolisport.cn/','直播')
test('5爱体育','https://www.5aisport.com/','直播')
test('极光','https://jiguangtiyu.com/','直播')
test('我的个人网站','http://www.yj-example.cn/','严靖')