利用Pycurl库监控WEB访问质量-邮件模板优化(三)

上一篇文章《利用Pycurl库监控WEB访问质量(二)》中成功的实现了通过邮件报警的功能,但是对于发送邮件却有很多的限制:为了邮件美观,一般都会制作成html邮件进行发送。但是发过HTML邮件的同学应该知道,电子邮箱中的HTML页面与普通的HTML页面比较在相当多的限制:

  • 不能引用外部CSS,所以只能写在页面里面
  • 不能引用外部JS,或者干脆禁用JS脚本

基于上述限制,我在《利用Pycurl库监控WEB访问质量(二)》中将HTML邮件代码直接和Python代码混在一起,细心的同学可以发现代码变得非常的混乱和冗长,实在难以接受。因此想个办法来进行优化。
思路:

  1. 如何将python代码和HTML代码分离开来?我想到了在平常的WEB开发中,不可避免的会使用到模板。因此模板是否也能适用在我发邮件上呢?经过思考,我决定使用jinja2(http://docs.jinkan.org/docs/jinja2/templates.html) 来使用
  2. 将HTML代码单独生成一个文件,然后监控脚本上将数据通过参数传递给模板(当然是利用了jinja2模板引擎的功能啦)
  3. 将最终渲染的html代码通过发送邮件的形式发出来即可。

开始动手吧,第一步生成邮件HTML,将原python文件中的HTML相关代码移出来,保存成mail.html(这我里叫mail.html,可以随意命名)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>{{ title }}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0 " />
    <meta name="format-detection" content="telephone=no" />
    <!--[if !mso]><!-->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
    <!--<![endif]-->
    <style type="text/css">
      body {
      -webkit-text-size-adjust: 100% !important;
      -ms-text-size-adjust: 100% !important;
      -webkit-font-smoothing: antialiased !important;
      }
      img {
      border: 0 !important;
      outline: none !important;
      }
      p {
      Margin: 0px !important;
      Padding: 0px !important;
      }
      table {
      border-collapse: collapse;
      mso-table-lspace: 0px;
      mso-table-rspace: 0px;
      }
      td, a, span {
      border-collapse: collapse;
      mso-line-height-rule: exactly;
      }
      .ExternalClass * {
      line-height: 100%;
      }
      span.MsoHyperlink {
      mso-style-priority:99;
      color:inherit;}
      span.MsoHyperlinkFollowed {
      mso-style-priority:99;
      color:inherit;}
      @media only screen and (min-width:481px) and (max-width:599px) {
      table[class=em_main_table] {
      width: 100% !important;
      }
      table[class=em_wrapper] {
      width: 100% !important;
      }
      td[class=em_hide], br[class=em_hide] {
      display: none !important;
      }
      img[class=em_full_img] {
      width: 100% !important;
      height: auto !important;
      }
      td[class=em_align_cent] {
      text-align: center !important;
      }
      td[class=em_pad_top]{
      padding-top:20px !important;
      }
      td[class=em_aside]{
      padding-left:10px !important;
      padding-right:10px !important;
      }
      td[class=em_height]{
      height: 20px !important;
      }
      td[class=em_space]{
      width:10px !important;    
      }
      td[class=em_width55] {
      width:80px !important;
      text-align:center !important;
      }
      td[class=em_width75] {
      width:100px !important;
      }
      td[class=em_font]{
      font-size:14px !important;    
      }
      td[class=em_font2] {
      text-align:center !important;
      }
      td[class=em_align_cent1] {
      text-align: center !important;
      padding-bottom: 10px !important;
      }
      }
      @media only screen and (max-width:480px) {
      table[class=em_main_table] {
      width: 100% !important;
      }
      table[class=em_wrapper] {
      width: 100% !important;
      }
      td[class=em_hide], br[class=em_hide], span[class=em_hide] {
      display: none !important;
      }
      img[class=em_full_img] {
      width: 100% !important;
      height: auto !important;
      }
      td[class=em_align_cent] {
      text-align: center !important;
      }
      td[class=em_pad_top]{
      padding-top:20px !important;
      }
      td[class=em_height]{
      height: 20px !important;
      }
      td[class=em_aside]{
      padding-left:10px !important;
      padding-right:10px !important;
      } 
      td[class=em_font]{
      font-size:14px !important;
      line-height:28px !important;
      }
      td[class=em_font1]{
      font-size:14px !important;
      line-height:18px !important;
      }
      td[class=em_font2]{
      font-size:14px !important;
      line-height:18px !important;
      text-align:center !important;
      }
      td[class=em_space]{
      width:10px !important;    
      }
      span[class=em_br]{
      display:block !important;
      }
      td[class=em_width55] {
      width:55px !important;
      font-size:15px !important;
      line-height:19px !important;
      text-align:center !important;
      }
      td[class=em_width75] {
      width:75px !important;
      font-size:15px !important;
      line-height:19px !important;
      }
      td[class=em_align_cent1] {
      text-align: center !important;
      padding-bottom: 10px !important;
      }
      }
    </style>
  </head>
  <body style="margin:0px; padding:0px;" bgcolor="#ffffff">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
      <!-- BODY  -->
      <tr>
        <td align="center" valign="top"  bgcolor="#ffffff">
          <table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
            <!-- === LOGO SECTION === -->
            <tr>
              <td height="40" class="em_height"> </td>
            </tr>
            <tr>
              <td align="center"><a href="#" target="_blank" style="text-decoration:none;">![LoGo Here](http://upload-images.jianshu.io/upload_images/3900471-558fa222f990324d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></td>
            </tr>
            <tr>
              <td height="30" class="em_height"> </td>
            </tr>
            <!-- === //LOGO SECTION === -->
            <!-- === NEVIGATION SECTION === -->
            <tr>
              <td height="1" bgcolor="#fed69c" style="font-size:0px; line-height:0px;">![](https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif)</td>
            </tr>
            <tr>
              <td height="1" bgcolor="#fed69c" style="font-size:0px; line-height:0px;">![](https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif)</td>
            </tr>
            <!-- === //NEVIGATION SECTION === -->
            <!-- THANK YOU SECTION -->
            <tr>
              <td valign="top" class="em_aside">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td height="27" class="em_height"> </td>
                  </tr>
                  <tr>
                    <td valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:22px; font-weight:bold; color:#30373b; text-transform:uppercase;">{{ subtitle }}</td>
                  </tr>
                  <tr>
                    <td height="23" class="em_height"> </td>
                  </tr>
                  <tr>
                    <td valign="top">
                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td valign="top">
                            <table width="290" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" bgcolor="#f6f7f8">
                              <tr>
                                <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                  <span style="color:#feae39;">机房信息 :</span> {{ location }}
                                </td>
                              </tr>
                            </table>
                            <table width="290" border="0" cellspacing="0" cellpadding="0" align="right" class="em_wrapper">
                              <tr>
                                <td valign="top" class="em_pad_top">
                                  <table width="100%" border="0" cellspacing="0" cellpadding="0"  bgcolor="#f6f7f8" >
                                    <tr>
                                      <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                        <span style="color:#feae39;">机房电话 :</span> {{ jftel}}
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                      </table>
                    </td>
                  </tr>
                  <tr>
                    <td height="20" style="font-size:1px; line-height:1px;"> </td>
                  </tr>
                  <tr>
                    <td valign="top">
                      <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8">
                        <tr>
                          <td width="25" class="em_space"> </td>
                          <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td height="12" style="font-size:1px; line-height:1px;"> </td>
                              </tr>
                              <tr>
                                <td align="left" valign="top"  class="em_font1" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:20px; font-weight:bold; color:#feae39; text-transform:uppercase;">{{ username }} :  <span style=" color:#30373b;">{{ telephone}}  |  {{email}}</span></td>
                              </tr>
                              <tr>
                                <td height="12" style="font-size:1px; line-height:1px;"> </td>
                              </tr>
                            </table>
                          </td>
                          <td width="25" class="em_space"> </td>
                        </tr>
                      </table>
                    </td>
                  </tr>
                  <tr>
                    <td height="25" class="em_height"> </td>
                  </tr>
                  <tr>
                    <td valign="top">
                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                              <tr>
                                <td width="30" class="em_hide"> </td>
                                <td width="255" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b; font-weight:bold;">网站</td>
                                <td width="5"></td>
                                <td width="35" class="em_width55" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b; font-weight:bold;">Code</td>
                                <td width="138" class="em_hide"></td>
                                <td width="5"></td>
                                <td align="right" class="em_width75" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b; font-weight:bold;">响应时间</td>
                                <td width="30" class="em_hide"> </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td height="14" style="font-size:1px; line-height:1px;"> </td>
                        </tr>
                        <tr>
                          <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                              {% for site in status %}
                                <tr>
                                  <td width="30" class="em_hide"> </td>
                                  <td width="255" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;"><a href="http://www.daoluyunshu.com">{{ site[0]}}</a></td>
                                  <td width="5"></td>
                                  <td width="35" class="em_width55" valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">{{ site[1] }}</td>
                                   <td width="138" class="em_hide"></td>
                                  <td width="5"></td>
                                  <td align="right" valign="top" class="em_width75" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">{{ site[2] }}</td>
                                  <td width="30" class="em_hide"> </td>
                                </tr>
                              {% endfor %}
                              <!-- <tr>
                                <td width="30" class="em_hide"> </td>
                                <td width="255" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;"><a href="http://www.daoluyunshu.com">http://www.daoluyunshu.com</a></td>
                                <td width="5"></td>
                                <td width="35" class="em_width55" valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">200</td>
                                 <td width="138" class="em_hide"></td>
                                <td width="5"></td>
                                <td align="right" valign="top" class="em_width75" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">0.287s</td>
                                <td width="30" class="em_hide"> </td>
                              </tr> -->
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td height="14" style="font-size:1px; line-height:1px;"> </td>
                        </tr>
                        <tr>
                          <td height="8" class="em_hide" style="font-size:1px; line-height:1px;"> </td>
                        </tr>
                        <tr>
                          <td height="1" bgcolor="#eaebeb" style="font-size:0px;line-height:0px;">![](https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif)</td>
                        </tr>
                        <tr>
                          <td height="14" style="font-size:1px; line-height:1px;"> </td>
                        </tr>
                        <tr>
                          <td height="11" class="em_hide" style="font-size:1px; line-height:1px;"> </td>
                        </tr>
                        <tr>
                          <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                              <tr>
                                <td align="right" style="font-family:'Open Sans', Arial, sans-serif; font-size:20px; line-height:24px; color:#30373b; font-weight:bold;">总网站数:    {{ num_sites }}  个</td>
                                <td width="30" class="em_hide"> </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
<!--                         <tr>
                          <td height="28" class="em_height"> </td>
                        </tr> -->
                      </table>
                    </td>
                  </tr>
                  <tr>
                    <td height="20" class="em_height"> </td>
                  </tr>
                </table>
              </td>
            </tr>
            <!-- //THANK YOU SECTION -->
          </table>
        </td>
      </tr>
      <!-- //BODY  -->
      <!-- === FOOTER SECTION === -->
      <tr>
        <td align="center" valign="top"  bgcolor="#30373b" class="em_aside">
          <table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
            <tr>
              <td height="22" class="em_height"> </td>
            </tr>
            <tr>
              <td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:12px; line-height:18px; color:#848789; text-transform:uppercase;">
               <span style="text-decoration:underline;"><a href="#" target="_blank" style="text-decoration:underline; color:#848789;">PRIVACY STATEMENT</a></span>   |   <span style="text-decoration:underline;"><a href="#" target="_blank" style="text-decoration:underline; color:#848789;">TERMS OF SERVICE</a></span><span class="em_hide">   |   </span><span class="em_br"></span><span style="text-decoration:underline;"><a href="#" target="_blank" style="text-decoration:underline; color:#848789;">RETURNS</a></span>
              </td>
            </tr>
            <tr>
              <td height="10" style="font-size:1px; line-height:1px;"> </td>
            </tr>
            <tr>
              <td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:12px; line-height:18px; color:#848789;text-transform:uppercase;">
                ©2‌016 XXX.net. All Rights Reserved.
              </td>
            </tr>
            <tr>
              <td height="10" style="font-size:1px; line-height:1px;"> </td>
            </tr>
            <tr>
              <td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:12px; line-height:18px; color:#848789;text-transform:uppercase;">
                If you do not wish to receive any further emails from us, please  
              </td>
            </tr>
            <tr>
              <td height="25" class="em_height"> </td>
            </tr>
          </table>
        </td>
      </tr>
      <!-- === //FOOTER SECTION === -->
    </table>
    <div style="display:none; white-space:nowrap; font:20px courier; color:#ffffff; background-color:#ffffff;">                                         </div>
  </body>
</html>

第二步,改造python代码:

# coding: utf-8
import pycurl
try:
    from io import BytesIO
except ImportError:
    from StringIO import StringIO as BytesIO
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import jinja2

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

templateLoader = jinja2.FileSystemLoader(searchpath=".")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = 'receipt.html'
template = templateEnv.get_template(TEMPLATE_FILE)
templateVars = {'title': 'XX监控邮件',
                'subtitle': 'XX网站监控信息记录',
                'location': 'XX数据中心',
                'jftel': '138XXXXXXXX',
                'username': 'XXX',
                'telephone': 'XXXX',
                'email': 'XXX@XXX.COM',
                'status': [],
                'num_sites': 0}

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 = "XXX@XXX.com"
to_addr = "XXX@XXX.COM"  # 发送给多个用户,使用逗号分隔
password = "XXX"
smtp_server = "smtp.XXX.com"


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()

flag = False    # 决定是否发报警邮件的标志
list_of_sites = []
outputText = ''

with open("sites.txt") as f:
    list_of_sites = f.readlines()

num_sites = len(list_of_sites)

templateVars['num_sites'] = num_sites

buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.FRESH_CONNECT, 1)

for site in list_of_sites:
    if len(site) == 0 or site.startswith("#"):
        pass
    else:
        c.setopt(c.URL, site.replace('\n', ''))
        c.setopt(c.WRITEFUNCTION, buffer.write)
        try:
            c.perform()
            http_code = c.getinfo(c.HTTP_CODE)
            total_time = c.getinfo(c.TOTAL_TIME)
            if http_code == 200:
                if total_time <= 5.0:
                    templateVars['status'].append([site, http_code, total_time])
                    # outputText = template.render(templateVars)
                else:
                    templateVars['status'].append([site, http_code, total_time])
                    # outputText = template.render(templateVars)
                    flag = True
            else:
                templateVars['status'].append([site, http_code, total_time])
                # outputText = template.render(templateVars)
                flag = True
        except pycurl.error, error:
            errno, errstr = error
            print errno, errstr
            templateVars['status'].append([site, errno, errstr])
            # outputText = template.render(templateVars)
            flag = True
c.close()
print templateVars

outputText = template.render(templateVars)
msg = format_msg(outputText)
if flag:
    send_email(msg)

最后的邮件如下:

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容