#!/usr/bin/python
#coding:utf-8
__author__ = 'dwj'
import getopt,sys,urllib,re
class monitor:
def __init__(self):
pass
#print '这是一个监控脚本,输入url和你希望得到的标题页面,如果是你设定的页面就返回OK,否则返回你想要的内容,也可以发告警邮件'
def sgethtml(self,url):
self.url=url
page=urllib.urlopen(url)
html=page.read()
return html
def monitor(self,title,html):
self.title=title
self.html=html
find_title=r'<TITLE>(.*)</TITLE>'
find_titled=re.compile(find_title,re.I)
t=re.findall(find_titled,html) #找到的是一个列表
t=t[0].strip() #去除前后空格
if t==title:
print 'The title is find! ok'
sys.exit(0)
else:
print t.decode('utf-8')
print 'The title is not find!! pls check it!'
sys.exit(3)
def useage():
print('This is a test fun')
#print 'hhh'
m=monitor()
try:
options,args=getopt.getopt(sys.argv[1:],'u:t:',['url=','title='])
except getopt.GetoptError:
sys.exit(2)
#print options,args
for name,value in options:
if name in ('-u','--url'):
url=value
if name in ('-t','--title'):
title=value
html=m.sgethtml(url)
m.monitor(title,html)