一则小脚本(工作中用)
大周一的上班第一件事,老大喊我,让我写个脚本,检测三个接口,我也是一脸无奈,要啥 ,就一个 让我可以收到事实的结果,那还能说啥,写脚本呗。
我能干啥,我不能干啥,我还是写脚本吧我,
先思考,我就访问接口 返回状态吗是200我就认为你过,不是200你就给我发邮件,好,需求整理好,找api文档,写代码,调试。
import requests ,time import smtplib,time,os from email.mime.text import MIMEText from email.utils import formataddr,parseaddr from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase def sendemali(mail_body): #发送email from_addr='leileili126@163.com' password='你的passwo' mail_to='接受邮件的' msg = MIMEMultipart() msg['Subject'] = '监控' msg['From'] ='监控反馈' msg['To'] = mail_to msg['Date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') txt = MIMEText(mail_body,'plain','gb2312') msg.attach(txt) smtp = smtplib.SMTP() server = smtplib.SMTP_SSL("smtp.163.com",465) server.login(from_addr, password) server.sendmail(from_addr, mail_to, msg.as_string()) server.quit() def appindex(): url='#检测的URl' m=requests.get(url) if int(m.status_code) !=200: sendemali(mail_body='APP首页不能正常加载') print('APP首页不能正常加载') else: print('app首页可以正常访问') def index(): url='首页/' index=requests.get(url) if int(index.status_code) !=200: sendemali(mail_body='官网首页不能加载数据') print('官网首页不能加载数据') else: print('官网首页可以正常访问') def applogin(): url=’登录的地址' data={ 'user_name':'#参数', 'password':'aixuetang123', 。。。。参数 } applogin=requests.post(url,data) if int(applogin.status_code) !=200: sendemali(mail_body='APP登录不能正常加载') print('APP登录不能正常加载') else: print('app登录可以正常访问') if __name__ == '__main__': while True: applogin() index() appindex() time.sleep(60)
代码60秒运行一次
结果如下
测试邮件如下:
下午我就优化下,增加了统计时间
import requests ,time,datetime import smtplib,time,os from email.mime.text import MIMEText from email.utils import formataddr,parseaddr from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase def sendemali(mail_body): #发送email from_addr='发送邮件m' password='密码' mail_to='l接受的邮件.com' msg = MIMEMultipart() msg['Subject'] = '监控' msg['From'] ='监控反馈' msg['To'] = mail_to msg['Date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') txt = MIMEText(mail_body,'plain','gb2312') msg.attach(txt) smtp = smtplib.SMTP() server = smtplib.SMTP_SSL("smtp.163.com",465) server.login(from_addr, password) server.sendmail(from_addr, mail_to, msg.as_string()) server.quit() def appindex(): url='app首页' m=requests.get(url) if int(m.status_code) !=200: sendemali(mail_body='APP首页不能正常加载,时间是:%s'%datetime.datetime.now()) print('APP首页不能正常加载') else: print('app首页可以正常访问') def index(): url='官网首页/' index=requests.get(url) if int(index.status_code) !=200: sendemali(mail_body='官网首页不能加载数据,时间是:%s'%datetime.datetime.now()) print('官网首页不能加载数据') else: print('官网首页可以正常访问') def applogin(): url='http登录界面in' data={ 'user_name':'zaixian19', 'password':'aixuetang123', ###参数 } applogin=requests.post(url,data) if int(applogin.status_code) !=200: sendemali(mail_body='APP登录不能正常加载,时间是%s'%datetime.datetime.now()) print('APP登录不能正常加载') else: print('app登录可以正常访问') if __name__ == '__main__': while True: applogin() index() appindex() time.sleep(60)
修改后的邮件报告
ps 老大让我写完 一中午接受到20封邮件,看来又要加班了。压测迫切需要