博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一则小脚本(工作中用)
阅读量:4482 次
发布时间:2019-06-08

本文共 3077 字,大约阅读时间需要 10 分钟。

 

   大周一的上班第一件事,老大喊我,让我写个脚本,检测三个接口,我也是一脸无奈,要啥 ,就一个  让我可以收到事实的结果,那还能说啥,写脚本呗。

    我能干啥,我不能干啥,我还是写脚本吧我,

  先思考,我就访问接口  返回状态吗是200我就认为你过,不是200你就给我发邮件,好,需求整理好,找api文档,写代码,调试。

import requests ,timeimport  smtplib,time,osfrom  email.mime.text import MIMETextfrom email.utils import formataddr,parseaddrfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasedef 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,datetimeimport  smtplib,time,osfrom  email.mime.text import MIMETextfrom email.utils import formataddr,parseaddrfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasedef 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封邮件,看来又要加班了。压测迫切需要

转载于:https://www.cnblogs.com/leiziv5/p/7592624.html

你可能感兴趣的文章
fseek()
查看>>
Python学习笔记——PyQt控件中文字居中显示
查看>>
JAVA环境下利用solrj二次开发SOlR搜索的环境部署常见错误
查看>>
Beta阶段敏捷冲刺前准备
查看>>
mini web框架-3-替换模板
查看>>
Siamese Network简介
查看>>
第六节 MongoDB 状态监控、备份复制及自动分片
查看>>
svg学习(三)rect
查看>>
博客园博文生成章节目录
查看>>
ruby 模块 的引入
查看>>
CI Weekly #21 | iOS 持续集成快速入门指南
查看>>
xml 校验
查看>>
Jquery获取输入框属性file,ajax传输后端,下载图片
查看>>
深入浅出Visual_C动态链接库(Dll)编程(宋宝华)----整理(word)
查看>>
docker运行环境安装-后续步骤(二)
查看>>
Python学习——02-Python基础——【3集合与函数】
查看>>
NPOI导出excel表格应用
查看>>
tensorflow从入门到放弃-0
查看>>
解锁scott用户
查看>>
多态的理解
查看>>