一则小脚本(工作中用)

 

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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秒运行一次

结果如下

测试邮件如下:

下午我就优化下,增加了统计时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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封邮件,看来又要加班了。压测迫切需要

posted @   北漂的雷子  阅读(1220)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示