python网站监控脚本

#!/usr/bin/python2.7
# -*- coding: utf8 -*-

import json
import urllib2
import os
import sys
import smtplib
import string
import time
import datetime
from email.mime.text import MIMEText

def getBody():
f=open("list.txt","r")
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',"Content-Type": "application/json"}
str=""

for L in f:
name=L.split()[0]
url=L.split()[1]
try:
req = urllib2.Request(url=url,headers=headers)
res = urllib2.urlopen(req)
res = res.read()
result="\t".join([name,res])
str += result+"\n"
except Exception,e:
print e
print url
str+="报错了"
#continue
f.close()
return str

def getCurrentDateTime():
now=int(time.time())
timeArray=time.localtime(now)
todayDate=time.strftime("%Y-%m-%d",timeArray)
todayTime=time.strftime("%H:%M:%S",timeArray)

todayDateTime=time.strftime("%Y-%m-%d %H:%M:%S",timeArray)
return todayDateTime

def SendMail(tomail,subject,body):
HOST="smtp.aaa.com"
SUBJECT=subject
TO=tomail
FROM="[email protected]"
text=body

msg=MIMEText(text,"plain","utf-8")
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = ','.join(TO)

try:
server=smtplib.SMTP()
server.connect(HOST,"25")
server.starttls()
server.login("[email protected]","bbbbbb")
server.sendmail(FROM,TO,msg.as_string())
server.quit()
print "邮件发送成功"
except Exception,e:
print "失败"+str(e)

if __name__ == "__main__":
#tomail=["[email protected]","[email protected]"] #list
tomail=["[email protected]"] #list
#tomail="[email protected]","[email protected]" #tuple
#tomail="[email protected],[email protected]" #string
#tomail=tomail.split(',')
#print tomail,type(tomail)
body=getBody()
subject="aaaa-%s"%getCurrentDateTime()
SendMail(tomail,subject,body)

你可能感兴趣的:(python网站监控脚本)