首页 > 编程语言 > 详细

Python监控网站接口值

时间:2016-12-23 16:49:13      阅读:242      评论:0      收藏:0      [点我收藏+]

Python监控网站接口值:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = liudong

import urllib,sys,urllib2,json,smtplib
from email.mime.text import MIMEText

mailto_list=[‘邮箱]
mail_host="smtp.philisense.com"
mail_user="邮箱"
mail_pass="邮箱密码"
mail_postfix="philisense.com"

def send_mail(to_list,sub,content):
     me="hello"+"<"+mail_user+"@"+mail_postfix+">"
     msg = MIMEText(content,_subtype=plain)
     msg[Subject] = sub
     msg[From] = me
     msg[To] = ";".join(to_list)
     try:
         server = smtplib.SMTP(mail_host,25)
         server.set_debuglevel(1)
         server.login(mail_user,mail_pass)
         server.sendmail(me, to_list, msg.as_string())
         server.close()
     except :
         return False
#把 接口:参数,以(key :value)写成字典的方式
api_host ={"http://back.oursdata.com/jppt/jp-index-data/hours-data":{"appid":"1,2,3,4",
        "indexType":"node_activeness_count",
        "dateType":"month",
        "startDate":"2016/6/25",
        "endDate":"2016/12/21"},
        "http://back.oursdata.com/jppt/jp-index-data/avg-day":{"appid":"1, 2, 3, 4, 5",
        "indexType":"active_avg",
        "dateType":"week",
        "_csrf":"Q2lwdWd0ekQaXkgANEEDMy89RVgORzsLJAs4MgVMKBYiOy8aBgZMCA=="}
        }
#给函数定义两个参数
def interfaceTest(api_url,parameter):
    params = urllib.urlencode(parameter)
    headers = {"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"}
    req = urllib2.Request(url=api_url, data=params, headers=headers)
    response = urllib2.urlopen(req)
    a = json.loads(response.read())
    if a["err"] == "ok":
        if len(a["data"]) == 0:
            send_mail(mailto_list,data,k + \n The data is None!!!)
    else:
        send_mail(mailto_list, err,"err" not equal to "ok")

if __name__ == "__main__":
    for (k,v) in api_host.items():
        interfaceTest(k,v)   #给函数传参

 

Python监控网站接口值

原文:http://www.cnblogs.com/liuyansheng/p/6215122.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!