鱼C论坛

 找回密码
 立即注册
查看: 1743|回复: 3

[技术交流] py2.7站点监控

[复制链接]
发表于 2016-10-9 20:58:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 domenet 于 2016-10-10 09:59 编辑

tkwebsite1.png
tkwebsite2.png
代码回复可见-.-#
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''WebSite Active Monitor  <By Domenet 2016-10-09 多线程版>'''
  4. import httplib,time,threading,Queue,tkMessageBox
  5. from Tkinter import *
  6. Flag=False#线程控制
  7. # Mark=False#线程状态
  8. normal=0#正常站点
  9. abnormal=0#异常站点
  10. def checkUrl(url):
  11.     global normal,abnormal
  12.     try:
  13.         httplib.socket.setdefaulttimeout(1)#定义http连接超时时间1秒
  14.         conn=httplib.HTTPConnection(url)#创建连接对象
  15.         conn.request('GET','/',headers={'Host':url})
  16.         r=conn.getresponse()
  17.         conn.close()
  18.         if r.status!=200 and r.reason!='OK':
  19.             abnormal+=1
  20.             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')
  21.         else:
  22.             normal+=1
  23.             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')
  24.     except Exception,e:
  25.         abnormal+=1
  26.         text.insert(0.0,'[%s] URL: http://%s %s\r\n'%(time.strftime('%H:%M:%S'),url.ljust(50),e),'tag2')
  27.     # finally:
  28.     #     print 'Thread [%d] Stop'%i

  29. def run(urllist):
  30.     global Flag,normal,abnormal
  31.     text.delete(0.0,END)
  32.     text.insert(END,u'[%s]服务启动成功!\r\n'%time.strftime('%H:%M:%S'),'tag1')
  33.     time.sleep(1)
  34.     while Flag:
  35.         text.delete(0.0,END)
  36.         normal=0
  37.         abnormal=0
  38.         for url in urllist:
  39.             if len(url)!=0:
  40.                 q.put(url)
  41.         countSite=q.qsize()
  42.         threadlist=[]     
  43.         for t in range(countSite):
  44.             threadlist.append(threading.Thread(target=startThread))
  45.         for t in threadlist:
  46.             t.start()
  47.             # print t.isAlive()
  48.         for t in threadlist:
  49.             t.join()   
  50.         countlbvar.set(u'[%s]  监控站点: %s  正常运行: %s  问题网站: %s  '%(time.strftime('%Y-%m-%d %H:%M:%S'),countSite,normal,abnormal))
  51.         time.sleep(10)

  52. def startThread():
  53.     while not q.empty():
  54.         url=q.get()
  55.         checkUrl(url)

  56. def gogogo():
  57.     global Flag
  58.     btnstop['state']='normal'
  59.     btnstart['state']='disabled'
  60.     url=entvar.get().strip()
  61.     if len(url)!=0:
  62.         urllist=url.split(';')
  63.         Flag=True
  64.         t=threading.Thread(target=run,args=(urllist,))
  65.         t.start()
  66.     else:
  67.         tkMessageBox.showwarning(u'提示',u'请先输入要监视的网址')
  68.         btnstart['state']='normal'
  69. def StopThred():
  70.     global Flag
  71.     Flag=False
  72.     btnstart['state']='normal'
  73.     btnstop['state']='disabled'
  74.     text.insert(END,u'[%s] 服务停止成功!\r\n'%time.strftime('%H:%M:%S'),'tag2')
  75.     countlbvar.set(u'[%s]  监控站点: null  正常运行: null  问题网站: null  ' % time.strftime('%Y-%m-%d %H:%M:%S'))
  76.    
  77. if __name__ == '__main__':
  78.     q=Queue.Queue()
  79.     root=Tk()
  80.     sb=Scrollbar(root)
  81.     entvar=StringVar()
  82.     countlbvar=StringVar()
  83.     root.title(__doc__)
  84.     root.geometry("800x300")
  85.     root.resizable(False, False)
  86.     lb=Label(root,text=u'地址:').grid(row=0,column=0,padx=5,pady=5)
  87.     entext=Entry(root,width=93,textvariable=entvar)
  88.     entext.grid(row=0,column=1,padx=2,pady=2)
  89.     btnstart=Button(root,text=u'开始',command=gogogo)
  90.     btnstart.grid(row=0,column=2,padx=2,pady=5)
  91.     btnstop=Button(root,text=u'停止',command=StopThred)
  92.     btnstop.grid(row=0,column=3,padx=2,pady=5)
  93.     btnstop['state']='disabled'
  94.     text=Text(root,height=18,yscrollcommand=sb.set)   
  95.     text.grid(row=1,columnspan=4,sticky=S+N+W+E,padx=2)
  96.     sb.grid(row=1,column=4,sticky=S+N+W)
  97.     sb.config(command=text.yview)
  98.     countlb=Label(root,textvariable=countlbvar,fg='blue')
  99.     countlb.grid(row=2,columnspan=4,sticky=W)
  100.     countlbvar.set(u'[%s]  监控站点: null  正常运行: null  问题网站: null '% time.strftime('%Y-%m-%d %H:%M:%S'))
  101.     text.tag_config('tag1',foreground='blue')
  102.     text.tag_config('tag2',foreground='red')
  103.     text.insert(END,'URL填写例: www.baidu.com;www.google.cn;www.qq.com 多个网址可用分号格开!\r\n','tag1')
  104.     root.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-10-15 19:03:33 | 显示全部楼层
顶楼主~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-30 18:29:14 | 显示全部楼层
楼主厉害!~!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-6 10:28:56 | 显示全部楼层
很牛逼的 东西应该多看看  好好哦啊好哦啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表