|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 domenet 于 2016-10-10 09:59 编辑
代码回复可见-.-#
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''WebSite Active Monitor <By Domenet 2016-10-09 多线程版>'''
- import httplib,time,threading,Queue,tkMessageBox
- from Tkinter import *
- Flag=False#线程控制
- # Mark=False#线程状态
- normal=0#正常站点
- abnormal=0#异常站点
- def checkUrl(url):
- global normal,abnormal
- try:
- httplib.socket.setdefaulttimeout(1)#定义http连接超时时间1秒
- conn=httplib.HTTPConnection(url)#创建连接对象
- conn.request('GET','/',headers={'Host':url})
- r=conn.getresponse()
- conn.close()
- if r.status!=200 and r.reason!='OK':
- abnormal+=1
- text.insert(0.0,'[%s] URL: http://%s Status:%s Reason:%s\r\n'%(time.strftime('%H:%M:%S'),url.ljust(50),r.status,r.reason),'tag2')
- else:
- normal+=1
- text.insert(END,'[%s] URL: http://%s Status:%s Reason:%s\r\n' %(time.strftime('%H:%M:%S'),url.ljust(50),r.status,r.reason),'tag1')
- except Exception,e:
- abnormal+=1
- text.insert(0.0,'[%s] URL: http://%s %s\r\n'%(time.strftime('%H:%M:%S'),url.ljust(50),e),'tag2')
- # finally:
- # print 'Thread [%d] Stop'%i
- def run(urllist):
- global Flag,normal,abnormal
- text.delete(0.0,END)
- text.insert(END,u'[%s]服务启动成功!\r\n'%time.strftime('%H:%M:%S'),'tag1')
- time.sleep(1)
- while Flag:
- text.delete(0.0,END)
- normal=0
- abnormal=0
- for url in urllist:
- if len(url)!=0:
- q.put(url)
- countSite=q.qsize()
- threadlist=[]
- for t in range(countSite):
- threadlist.append(threading.Thread(target=startThread))
- for t in threadlist:
- t.start()
- # print t.isAlive()
- for t in threadlist:
- t.join()
- countlbvar.set(u'[%s] 监控站点: %s 正常运行: %s 问题网站: %s '%(time.strftime('%Y-%m-%d %H:%M:%S'),countSite,normal,abnormal))
- time.sleep(10)
- def startThread():
- while not q.empty():
- url=q.get()
- checkUrl(url)
- def gogogo():
- global Flag
- btnstop['state']='normal'
- btnstart['state']='disabled'
- url=entvar.get().strip()
- if len(url)!=0:
- urllist=url.split(';')
- Flag=True
- t=threading.Thread(target=run,args=(urllist,))
- t.start()
- else:
- tkMessageBox.showwarning(u'提示',u'请先输入要监视的网址')
- btnstart['state']='normal'
- def StopThred():
- global Flag
- Flag=False
- btnstart['state']='normal'
- btnstop['state']='disabled'
- text.insert(END,u'[%s] 服务停止成功!\r\n'%time.strftime('%H:%M:%S'),'tag2')
- countlbvar.set(u'[%s] 监控站点: null 正常运行: null 问题网站: null ' % time.strftime('%Y-%m-%d %H:%M:%S'))
-
- if __name__ == '__main__':
- q=Queue.Queue()
- root=Tk()
- sb=Scrollbar(root)
- entvar=StringVar()
- countlbvar=StringVar()
- root.title(__doc__)
- root.geometry("800x300")
- root.resizable(False, False)
- lb=Label(root,text=u'地址:').grid(row=0,column=0,padx=5,pady=5)
- entext=Entry(root,width=93,textvariable=entvar)
- entext.grid(row=0,column=1,padx=2,pady=2)
- btnstart=Button(root,text=u'开始',command=gogogo)
- btnstart.grid(row=0,column=2,padx=2,pady=5)
- btnstop=Button(root,text=u'停止',command=StopThred)
- btnstop.grid(row=0,column=3,padx=2,pady=5)
- btnstop['state']='disabled'
- text=Text(root,height=18,yscrollcommand=sb.set)
- text.grid(row=1,columnspan=4,sticky=S+N+W+E,padx=2)
- sb.grid(row=1,column=4,sticky=S+N+W)
- sb.config(command=text.yview)
- countlb=Label(root,textvariable=countlbvar,fg='blue')
- countlb.grid(row=2,columnspan=4,sticky=W)
- countlbvar.set(u'[%s] 监控站点: null 正常运行: null 问题网站: null '% time.strftime('%Y-%m-%d %H:%M:%S'))
- text.tag_config('tag1',foreground='blue')
- text.tag_config('tag2',foreground='red')
- text.insert(END,'URL填写例: www.baidu.com;www.google.cn;www.qq.com 多个网址可用分号格开!\r\n','tag1')
- root.mainloop()
复制代码 |
|