A small script (used in work ).
The first thing to do when I went to work on Monday was to yell at me and ask me to write a script to detect three interfaces. I also looked helpless. What should I do, let me get the result of the fact. What else can I say? Write a script.
What can I do? I can't do anything. I should write a script,
First think about it. I will access the interface and return the status. If it is 200, I will think you have been there. If it is not 200, you will send me an email. Well, you need to sort out the requirements, find the api documentation, and write code, debugging.
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): # Send emailfrom_addr = 'leileili126 @ 163.com 'password = 'your passwo' mail _ to = 'Accept the mail' msg = MIMEMultipart () msg ['subobject'] = 'monitoring 'msg ['from'] = 'monitoring feedback' msg ['to'] = mail_tomsg ['date'] = ti Me. 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 = '# The detected URl' m = requests. get (url) if int (m. status_code )! = 200: sendemali (mail_body = 'app Homepage cannot be loaded normally ') print ('app Homepage cannot be loaded normally') else: print ('app homepage can be accessed normally ') def index (): url = 'homepage/'index = requests. get (url) if int (index. status_code )! = 200: sendemali (mail_body = 'data cannot be loaded on the homepage of the official website ') print ('data cannot be loaded on the homepage of the official website') else: print ('the homepage of the official website can be visited normally ') def applogin (): url = 'login address 'data = {'user _ name': '# parameter', 'Password': 'aixuetang123 ',.... Parameter} applogin = requests. post (url, data) if int (applogin. status_code )! = 200: sendemali (mail_body = 'app Login Failed to load normally ') print ('app Login Failed to load normally') else: print ('app login can be accessed normally ') if _ name _ = '_ main _': while True: applogin () index () appindex () time. sleep (60)
Code runs once every 60 seconds
The result is as follows:
The test email is as follows:
I optimized it in the afternoon and added the statistical time.
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): # Send emailfrom_addr = 'send mail m' password = 'Password' mail _ to = 'l received mail. com 'msg = MIMEMultipart () msg ['subobject'] = 'monitoring 'msg ['from'] = 'monitoring feedback' msg ['to'] = mail_tomsg ['date'] = time. str Ftime ('% 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 homepage'm = requests. get (url) if int (m. status_code )! = 200: sendemali (mail_body = 'app Homepage cannot be loaded normally, time: % s' % datetime. datetime. now () print ('app Homepage cannot be loaded normally ') else: print ('app homepage can be accessed normally') def index (): url = 'homepage/'index = requests. get (url) if int (index. status_code )! = 200: sendemali (mail_body = 'data cannot be loaded on the homepage of the official website. The time is % s' % datetime. datetime. now () print ('data cannot be loaded on the homepage of the official website ') else: print ('homepage can be accessed normally on the official website') def applogin (): url = 'HTTP logon interface in 'data = {'user _ name': 'zaixian19', 'Password': 'aixuetang123 ', ### parameter} applogin = requests. post (url, data) if int (applogin. status_code )! = 200: sendemali (mail_body = 'app Login Failed to load normally, time is % s' % datetime. datetime. now () print ('app login cannot load normally ') else: print ('app login can be accessed normally ') if _ name _ = '_ main _': while True: applogin () index () appindex () time. sleep (60)
Modified email report
Ps boss asked me to finish reading 20 emails at noon. It seems that I have to work overtime again. Stress Testing is urgently needed