Python 运维

利用Python Urllib2库监控WEB访问质量(一)

2016-12-01  本文已影响0人  iDefeng

先上脚本,再来解释,就是这么简单直接又暴力:

# coding:utf8
import urllib2
import time
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode("utf-8") if isinstance(addr, unicode) else addr))

from_addr = "XXXX"  # 发件人邮件地址
to_addr = "xxxx"  # 发送给多个用户,使用逗号分隔
password = "XXX"  # 发件人邮箱的密码
smtp_server = "XXX" #发件人邮箱所有SMTP服务器地址

def format_msg(s):
    # type: (object) -> object
    msg = MIMEText(s, _subtype='html', _charset='utf-8')
    msg['From'] = _format_addr(from_addr)
    msg['To'] = _format_addr(to_addr)
    msg['Subject'] = Header(u'来自xxx的监控小黑屋', 'utf-8').encode()
    return msg.as_string()

def send_email(s):
    server = smtplib.SMTP(smtp_server, 25)
    server.set_debuglevel(0)
    server.login(from_addr, password)
    server.sendmail(from_addr, to_addr, s)
    server.quit()

list_of_sites = []   # 需要被监控的网站列表
result_of_access = '''
    <html lang="zh-CN">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta http-equiv="x-ua-compatible" content="ie=edge">
            <style type="text/css">
                table{border-left: 1px solid #000;border-bottom: 1px solid #000;}
                td, tr, th {border-top: 1px solid #000000;border-right: 1px solid #000000;}
                * {font-size: 26px;}
            </style>
        </head>
        <body>
            <table>
                <thead>
                    <tr>
                        <th>访问网站</th>
                        <th>状态码</th>
                        <th>耗时(秒)</th>
                    </tr>
                </thead>
                <tbody>
'''
with open("sites.txt") as f:   #我们把网站列表放在了一个sites.txt里边
    list_of_sites = f.readlines()
# print list_of_sites
for site in list_of_sites:
    # print site
    start = time.time()
    try:
        f = urllib2.urlopen(site)
        c = time.time() - start
        if f.code == 200:   # 如果http code是200表示访问成功
            if c <= 5.0:   # 
                msg = "<tr><td><a href=" + site + "</a>" + site + "</td><td><font color='#00FF00'><b>" + str(f.code) \
                      + "</b></font></td><td>" + str(c) + "</td></tr>"
                result_of_access += msg
            else:
                msg = "<tr><td><a href=" + site + "</a>" + site + "</td><td><font color='#00FF00'><b>" + str(
                    f.code) + "</b></font></td><td><font color='#FF0000'>" + str(c) + "</font></td></tr>"
                result_of_access += msg
        else:
            msg = "<tr><td><a href=" + site + "</a>" + site + "</td><td><font color='#FF0000'><b>" + \
                  str(f.code) + "</b></font></td></tr>" + str(c) + "</td></tr>"
            result_of_access += msg
    except:
        msg = "<tr><td><a href=" + site + "</a>" + site + "</td><td><font color='#FF0000'>Fail</font></td><td>无法解析地址: " + site + "</td></tr>"
        result_of_access += msg
result_of_access += '''
                </tbody>
            </table>
            <p><font color="red">耗时超过5秒:表示访问可能有点缓慢,请重视</font></p>
            <p>无法解析错误:请及时处理,查找原因</p>
        </body>
    </html>
'''
msg = format_msg(result_of_access)
send_email(msg)

上一篇下一篇

猜你喜欢

热点阅读