- 需求构思: 在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负责显示接受到的实时数据
- 代码部分
# -*- 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()
function SocketClient(ip,port,query) { var _this = this; this.socket = ''; this.uid = 0; this.sign = ''; this.connect = function() { this.socket = new WebSocket('ws://'+ip+':'+port+'/'+query); this.socket.onopen = function() { _this.onOpen() } this.socket.onmessage = function(event) { data = event.data; data = data.split("<split>") _this.uid = data[0]; _this.sign = data[1]; text = data[2]; if(text!='SETUID') { _this.onData(text); } else { _this.onRegist() } } this.socket.onclose = function(event) { _this.onClose(); }; } this.onRegist = function() { } this.onClose = function() { } this.onOpen = function() { } this.onData = function(text) { } this.sendData = function (text) { var data = this.uid+'<split>'+this.sign+'<split>'+text this.socket.send(data); } this.close = function() { this.socket.close(); } }
<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,用户在浏览器查看运行的实时数据。