程式紀錄~使用python測試網頁是否正常

22 views
Skip to first unread message

win...@nyps.kh.edu.tw

unread,
Nov 14, 2018, 7:37:46 PM11/14/18
to windf...@googlegroups.com
# -*- coding: utf-8 -*-
'''
這支程式使用multi方式,
可用來開啟學校網頁,偵測網頁是否OK,偵測失敗的網頁會寄信通知!
在個人PC執行時間:9秒
設計:Windfly 
日期:2015-09-01
程式語言:Python2.7
'''

urls = [['馬公國中','http://www.mkjh.phc.edu.tw'], ['中正國中','http://www.ccjh.phc.edu.tw'], ['文光國中','http://www.wgjh.phc.edu.tw'],
        ['澎南國中','http://www.pnjh.phc.edu.tw'], ['志清國中','http://www.jcjh.phc.edu.tw'], ['白沙國中','http://www.psjh.phc.edu.tw'],
        ['西嶼國中','http://www.hyjh.phc.edu.tw'], ['吉貝國中','http://www.gbjh.phc.edu.tw'], ['鳥嶼國中','http://www.nyjh.phc.edu.tw'],
        ['望安國中','http://www.wajh.phc.edu.tw'], ['將澳國中','http://www.jajh.phc.edu.tw'], ['七美國中','http://www.cmjh.phc.edu.tw'],
        ['湖西國中','http://www.hsjh.phc.edu.tw'], ['鎮海國中','http://www.pnjh.phc.edu.tw'],        
        ['馬公國小','http://www.mkps.phc.edu.tw'], ['中興國小','http://www.chps.phc.edu.tw'], ['中正國小','http://www.ccps.phc.edu.tw'],
        ['文澳國小','http://www.wops.phc.edu.tw'], ['中山國小','http://www.csps.phc.edu.tw'], ['東衛國小','http://www.dwps.phc.edu.tw'],
        ['石泉國小','http://www.scps.phc.edu.tw'], ['山水國小','http://www.ssps.phc.edu.tw'], ['風櫃國小','http://www.fkps.phc.edu.tw'],
        ['虎井國小','http://www.hjps.phc.edu.tw'], ['興仁國小','http://www.sjps.phc.edu.tw'], ['果葉國小','http://www.gyps.phc.edu.tw'],
        ['西溪國小','http://www.sips.phc.edu.tw'], ['龍門國小','http://www.lmps.phc.edu.tw'], ['隘門國小','http://www.amps.phc.edu.tw'],
        ['沙港國小','http://www.skps.phc.edu.tw'], ['中屯國小','http://www.ctps.phc.edu.tw'], ['講美國小','http://www.jmps.phc.edu.tw'],
        ['成功國小','http://www.cgps.phc.edu.tw'], ['赤崁國小','http://www.ckps.phc.edu.tw'], ['吉貝國小','http://www.cpps.phc.edu.tw'],
        ['鳥嶼國小','http://www.nyps.phc.edu.tw'], ['港子國小','http://www.ktps.phc.edu.tw'], ['合橫國小','http://www.hhps.phc.edu.tw'],
        ['大池國小','http://www.dtps.phc.edu.tw'], ['文光國小','http://www.wgps.phc.edu.tw'], ['赤馬國小','http://www.rmps.phc.edu.tw'],
        ['花嶼國小','http://www.hyps.phc.edu.tw'], ['七美國小','http://www.cmps.phc.edu.tw'], ['雙湖國小','http://www.tlps.phc.edu.tw'],
        ['將軍國小','http://www.jjps.phc.edu.tw'], ['竹灣國小','http://www.cwps.phc.edu.tw'], ['湖西國小','http://www.hsps.phc.edu.tw'],
        ['五德國小','http://www.wdps.phc.edu.tw'], ['外垵國小','http://www.wips.phc.edu.tw'], ['後寮國小','http://www.hlps.phc.edu.tw'],
        ['時理國小','http://www.slps.phc.edu.tw'], ['望安國小','http://www.waps.phc.edu.tw'], ['錯誤國小','http://www.ppps.phc.edu.tw'],
        ['亂打國小','http://www.iips.phc.edu.tw'],
        ]

import gevent
from gevent import monkey
from datetime import datetime
from email.mime.text import MIMEText


monkey.patch_all()

import urllib2
import smtplib

def print_head(url):
    #print 'Starting %s' % url[0].decode('utf-8').encode('big5')
    try:
    	data = urllib2.urlopen(url[1]).read()
    except:
    	#print '%s: %s' % (url[0].decode('utf-8').encode('big5'), '網頁無法開啟'.decode('utf-8').encode('big5'))
        sendmail(url[0])
        return '%s: %s' % (url[0].decode('utf-8').encode('big5'), '網頁無法開啟'.decode('utf-8').encode('big5'))
    else:
    	#print '%s: %s bytes: %r' % (url[0].decode('utf-8').encode('big5'), len(data), data[:50])
    	pass

def sendmail(hostname):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    SEND_USER = 'xxxx...@gmail.com'
    SEND_PSWD = 'xxxxxxx'
    SEND_TO = [SEND_USER, 'xxx...@mail.phc.edu.tw']
    content = """這是學校網站掛掉的mail通知信。"""
    text_subtype = 'plain'
    MSG = MIMEText(content, text_subtype)
    MSG['Subject'] = hostname + "--- 學校網站偵測失敗。" 
    MSG['From']    = 'xxx...@gmail.com'

    try:
        server.login(SEND_USER, SEND_PSWD)
        server.sendmail(SEND_USER, SEND_TO, MSG.as_string())
    except Exception as ex:
        return "error" + "%s" % ex

    server.quit()


t1 = datetime.now() #開始時間
jobs = [gevent.spawn(print_head, url) for url in urls]
gevent.joinall(jobs)


i = 0
for x in jobs:
    if jobs[i].value:
        print jobs[i].value
    i += 1


total = datetime.now() - t1 #結束時間
print '%s: %s' % ('花費時間 '.decode('utf-8').encode('big5'), total)
Reply all
Reply to author
Forward
0 new messages