- A+
所属分类:python
需求构思: 在linux下常常需要查看程序的实时输出,我们用 tail -f logfile 即可在console下实现此需求。现在要拓宽应用: 想在web browser下查看程序(以及日志)的实时输出(也就是程序Log的Web实时监控)。
架构构思
因为考虑了“实时"这个需求,所以初步定位为socket架构; 再因为是构建在web之上,属于web app,所以socket进一步细分为:web socket。初步验证: web socket的 server部分: 可选: 1)自己实现 2) Node.js 3)其他框架(i.e. pywebsocketserver), web socket的client部分: 可选: 1) firefox 16 2) chrome 3)其他浏览器
架构实现
- Python里的subprocess可用pipe获取数据,再开子进程获取数据行
- Server负责把上述数据行缓存后再源源不断发送到 Client
- Client负责显示接受到的实时数据
python代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# -*- coding: utf8 -*- import sys import time import threading from pywebsocketserver.server import SocketServer from pywebsocketserver.baseio import BaseIO import subprocess def tcpdump(): global g_output_log popen=subprocess.Popen(['bash','-c',"/usr/sbin/tcpdump -i eth0 -v"],stdout=subprocess.PIPE,stderr=subprocess.PIPE) pid=popen.pid print('Popen.pid:'+str(pid)) while True: line=popen.stdout.readline().strip() #line =popen.communicate() print "output:%s" %(line) g_output_log.append(line) if subprocess.Popen.poll(popen) is not None: break print('DONE') class MyIO(BaseIO): def onData(self,uid,text): self.sendData(uid,"received the message:%s"%(text,)) def onConnect(self,uid): global g_uid g_uid=uid while True: #self.sendData(uid,"testing...") if len(g_output_log) >0: log = g_output_log.pop() self.sendData(uid,log) else: time.sleep(.01) try: g_output_log=[] g_uid=None tcpdump = threading.Thread(name='tcpdump', target=tcpdump) tcpdump.start() port = sys.argv[1] except: port = 88 port = int(port) myIo = MyIO() SocketServer(port,myIo).run() |
JS代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# -*- coding: utf8 -*- import sys import time import threading from pywebsocketserver.server import SocketServer from pywebsocketserver.baseio import BaseIO import subprocess def tcpdump(): global g_output_log popen=subprocess.Popen(['bash','-c',"/usr/sbin/tcpdump -i eth0 -v"],stdout=subprocess.PIPE,stderr=subprocess.PIPE) pid=popen.pid print('Popen.pid:'+str(pid)) while True: line=popen.stdout.readline().strip() #line =popen.communicate() print "output:%s" %(line) g_output_log.append(line) if subprocess.Popen.poll(popen) is not None: break print('DONE') class MyIO(BaseIO): def onData(self,uid,text): self.sendData(uid,"received the message:%s"%(text,)) def onConnect(self,uid): global g_uid g_uid=uid while True: #self.sendData(uid,"testing...") if len(g_output_log) >0: log = g_output_log.pop() self.sendData(uid,log) else: time.sleep(.01) try: g_output_log=[] g_uid=None tcpdump = threading.Thread(name='tcpdump', target=tcpdump) tcpdump.start() port = sys.argv[1] except: port = 88 port = int(port) myIo = MyIO() SocketServer(port,myIo).run() |
html代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script src="socketclient.js"></script> </head> <body> <script> var client = new SocketClient("192.168.199.3",88,"chat"); client.connect(); client.onData = function(text) { console.log(text); element=document.getElementById("log"); if(element){ if(element.value.length >100000) { element.value=""; } element.value += (text + "\n"); element.scrollTop = element.scrollHeight; } } client.onRegist = function() { this.sendData("I am coming!"); } </script> <textarea name="log" id="log" style="font-size:12px; width:100%; height: 600px;">Tcpdump log:</textarea> </body> </html> |
演示说明
Server运行tcpdump,用户在浏览器查看运行的实时数据。
项目地址
https://github.com/suxianbaozi/pywebsocketserver
或者本站下载:[file:http://www.ttlsa.com/wp-content/uploads/2015/09/pywebsocketserver-master.zip]

微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
08/12/2016 下午 2:16 沙发
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下goeasy,java后台推送只需要两行代码, js前端推送也只需要3,4行,而且文档齐全,还提供了后台查询信息收发情况,所以我觉得GoEasy推送服务是个不错的选择。官网: https://goeasy.io/
30/09/2015 上午 9:34 板凳
非常好。。。
17/09/2015 下午 4:57 地板
这儿有个界面更好看的,原理差不多,http://logio.org/
13/04/2016 下午 1:59 1层
@无影风随 这个logio我在github上下载下来,感觉缺文件,跑不起来啊,你弄好了吗?
14/09/2015 下午 12:25 4楼
支持白开大神,掌声 :wink: