python监控接口请求

 

 

 

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
58
59
60
61
62
63
64
65
66
67
1 #!/usr/bin/env python
 2 #coding=utf8
 3 import time,os,sched,urllib,httplib
 4 import smtplib
 5 import string
 6
 7 schedule = sched.scheduler(time.time, time.sleep)
 8 def perform_command(self, inc):
 9     schedule.enter(inc, 0, perform_command, (self, inc))
10     #os.system(cmd)
11     monitoring(self)
12 def timming_exe(self, inc = 60):
13     schedule.enter(inc, 0, perform_command, (self, inc))
14     schedule.run()
15
16 def monitoring(self):
17     print("开始监控...")
18     httpClient = None
19     try:
20         params = urllib.urlencode({'name': 'tom', 'age': 22})
21         headers = {"Content-type": "application/x-www-form-urlencoded"
22                     , "Accept": "text/plain"}
23 
24         httpClient = httplib.HTTPConnection("2xx.x9.2x1.x", 8800, timeout=30)
25         httpClient.request("POST", "/path/pathxxxxxxx", params, headers)
26 
27         response = httpClient.getresponse()
28         print (response.status)
29         print (response.reason)
30         #print response.read()
31         #print response.getheaders() #获取头信息
32        
33         if response.status == 200:
34             print (u"正常")
35         else:
36             print (u"异常")
37             sendmsg
38             print '邮件已发送....'
39     except Exception,e:
40         print e         
41     finally:
42         if httpClient:
43             httpClient.close()
44
45 def sendmsg():   
46     FROM="xxx.com"
47     TO="xxx.com"
48     PASS="xxx"
49     HOST="smtp.sina.com"
50     PORT="25"
51     SUBJECT="Interface alarm "
52     TEXT="The alarm information !"
53     BODY= string.join((
54             "From: %s" % FROM,
55             "To: %s" % TO,
56             "Subject: %s" % SUBJECT,
57             "",
58             TEXT
59     ), "\r\n")
60     server=smtplib.SMTP()
61     server.connect(HOST,"25")
62     server.login(FROM,PASS)
63     server.sendmail(FROM,TO,BODY)
64     server.quit()
65        
66 print("服务监控>>> 一分钟后开始执行(每10秒):")
67 timming_exe("echo %time%", 10)

  

 

作者:medivhxu

出处:https://www.cnblogs.com/medivhxu/p/6856771.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   Medivh_Xu  阅读(5491)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题