zoukankan      html  css  js  c++  java
  • python监控接口请求

     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     ), "
    ")
    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)
    

      

  • 相关阅读:
    Vue.directive()方法创建全局自定义指令
    vue中通过ref属性来获取dom的引用
    v-cloak指令
    v-if和v-show
    vue中的v-on事件监听机制
    vue指令v-model
    vue中v-for系统指令的使用
    从零开始在虚拟机中搭建一个4个节点的CentOS集群(一)-----下载及配置CentOS
    MySQL-数据库创建与删除
    MySQL-PREPARE语句
  • 原文地址:https://www.cnblogs.com/medivhxu/p/6856771.html
Copyright © 2011-2022 走看看