好用的python网站监控程序



#!/usr/bin/python

#coding=utf8

'''

Created on 2012-8-17


@author: jiangxh


Description:


'''


import httplib

#用类来包装线程对象

import threading

import time,datetime

import smtplib 


def http_open(url):

    try:

        conn = httplib.HTTPConnection(url)

        conn.request("GET", "/")

        r=conn.getresponse()

        return r.status  

    except Exception,e:

        return e


from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.Utils import COMMASPACE, formatdate 


#server['host'], server['user'], server['passwd']

def send_mail(text): 


    msg = MIMEMultipart() 

    msg['From'] = "发件人"

    msg['Subject'] = u"不好了网站挂了" 

    msg['To'] = COMMASPACE.join("收件人")

    msg['Date'] = formatdate(localtime=True) 

    msg.attach(MIMEText(text,'html','GBK')) 

    smtp = smtplib.SMTP("server") 

    #smtp.set_debuglevel(1)

    smtp.login("用户名", "密码") 

    smtp.sendmail("发件人", "收件人", msg.as_string()) 

    smtp.close()


class check(threading.Thread): 

    def __init__(self, url ,interval):

        threading.Thread.__init__(self)

        self.interval = interval

        self.url=url

 

    def run(self): #Overwrite run() method, put what you want the thread do here

        

        status = http_open(self.url)

        if status==200:

            print u"链接[%s]响应正常,状态为:%s"%(self.url,status)

        elif status==302:

            print u"链接[%s]响应正常,状态为:%s"%(self.url,status)

        else:

            print u"链接[%s]响应异常,状态为:%s"%(self.url,status)

            send_mail(u"链接[%s]响应异常,状态为:%s"%(self.url,status))

        time.sleep(self.interval)


def main():

    urls=["www.hzins.com",

          "www.baoyuntong.com"

          ]

    thread_list =[]


    print u"*********************************"

    print u"欢迎使用: 慧择经纪保险网站监控程序"

    print u"*********************************"

    print u"共有%s个网站需要检测"%len(urls)

    

    while(True):

        print u"执行时间:%s"%(datetime.datetime.now())

        for url in urls:

            thread = check(url,10)

            thread.start()

            thread_list.append(thread)

        time.sleep(120)

        print "\n\n"

        

if __name__ == "__main__":

    main()


转自:http://hi.baidu.com/design231/item/c3a51a008046aac674cd3cd8
by:小飞虎BS

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值