基于python的websocket开发,tomcat日志web页面实时打印监控案例
web socket 接收器:webSocket.py
相关依赖
# pip install bottle gevent gevent-websocket argparse
from bottle import request, Bottle, abort
from geventwebsocket import WebSocketError
from gevent.pywsgi import WSGIServer
from flask import request
from geventwebsocket.handler import WebSocketHandler
from bottle import get, post, request
app = Bottle()
users = {}
@app.get('/websocket/<token>/<senduser>')
def handle_websocket(token,senduser):
wsock = request.environ.get('wsgi.websocket')
users[token] = wsock
if not wsock:
abort(, 'Expected WebSocket request.')
while True:
try:
message = wsock.receive()
except WebSocketError:
breakif message:
try:
users[senduser].send(message)
except WebSocketError:
print u'kill'
server = WSGIServer(("0.0.0.0", ), app,handler_class=WebSocketHandler)
server.serve_forever()
服务端:logs.py
相关依赖:
pip install websocket-client
import subprocess
from websocket import create_connection
ws_server = "ws://172.18.30.19:1010/websocket/<token>/<senduser>"
ws = create_connection(ws_server) command='sshpass -p 123456 ssh 192.168.20.200 -p 32776 -o StrictHostKeychecking=no "tail -f /root/tomcat-8.0/logs/catalina.out"'
popen=subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) while True:
line=popen.stdout.readline().strip()
ws.send(line)
应用前端:logs.html
<!DOCTYPE html>
<html>
<head>
<title>LOG+</title>
<style>
html, body {
font: normal .9em arial, helvetica;
}
#log {
width: 440px;
height: 200px;
border: 1px solid #7F9DB9;
overflow: auto;
}
#msg {
width: 330px;
}
</style>
<script>
var socket;
function init() {
var host = "ws://172.18.30.19:1010/websocket/<token>/<senduser>";
try {
socket = new WebSocket(host);
socket.onopen = function (msg) {
log('Connected');
};
socket.onmessage = function (msg) {
log(msg.data);
};
socket.onclose = function (msg) {
log("Lose Connection!");
};
socket.onerror = function(msg) {
log("websocket error!");
};
}
catch (ex) {
log(ex);
}
$("msg").focus();
}
function send() {
var txt, msg;
txt = $("msg");
msg = txt.value;
if (!msg) {
alert("Message can not be empty");
return;
}
txt.value = "";
txt.focus();
try {
socket.send(msg);
} catch (ex) {
log(ex);
}
}
window.onbeforeunload = function () {
try {
socket.send('quit');
socket.close();
socket = null;
}
catch (ex) {
log(ex);
}
};
function $(id) {
return document.getElementById(id);
}
function log(msg) {
$("log").innerHTML += "<br>" + msg;
}
function onkey(event) {
if (event.keyCode == ) {
send();
}
}
</script>
</head>
<body onload="init()">
<h3>WebSocket</h3>
<br><br>
<div id="log"></div>
<input id="msg" type="textbox" onkeypress="onkey(event)"/>
<button onclick="send()">发送</button>
</body>
</html>
测试一把:

结束语:
前端 ——》 接收器 《—— 服务端
以需求用例为基,Case&Coding两条线并行,服务(M)&消费(VC)分离,单元、接口、功能、集成四层质量管理,自动化集成、测试、交付全程支持。 3个大阶段(需求分析阶段、研发准备阶段、研发测试阶段)16个小历程(*)确定好边界,明确好对接产物,做好服务管理。
在使用这段python代码之前,先在安装以下模块:
pip install gevent-websocket
pip install gevent
pip install bottle
- from bottle import request, Bottle, abort
- from geventwebsocket import WebSocketError
- from gevent.pywsgi import WSGIServer
- from geventwebsocket.handler import WebSocketHandler
- app = Bottle()
- users = set()
- @app.get('/websocket/')
- def handle_websocket():
- wsock = request.environ.get('wsgi.websocket')
- users.add(wsock)
- if not wsock:
- abort(400, 'Expected WebSocket request.')
- while True:
- try:
- message = wsock.receive()
- except WebSocketError:
- break
- print u"现有连接用户:%s" % (len(users))
- if message:
- for user in users:
- try:
- user.send(message)
- except WebSocketError:
- print u'某用户已断开连接'
- # 如果有客户端断开,则删除这个断开的websocket
- users.remove(wsock)
- server = WSGIServer(("0.0.0.0", 8000), app,handler_class=WebSocketHandler)
- server.serve_forever()
这2篇文章的websocket服务端代码,都差不多,只是用的模块不一样而已,前者用的是bottle-websocket扩展包,此篇用的是gevent-websocket包
其实还可以用websocket,开发成一个聊天室的,很简单,只是html和javascripts要多开发点功能而已,有兴趣的自己试试
如何在python终端中调试呢,先要安装一个python的websocket-client包(pip install websocket-client),以下是相关的调试代码:
- >>> from websocket import create_connection
- >>> ws_server = "ws://192.168.1.221:8000/websocket/"
- >>> ws = create_connection(ws_server)
- >>> ws.send('hello,http://www.linuxyw.com') #发送数据
- 34
- >>> ws.recv() #获取数据
- 'hello,http://www.linuxyw.com'
- >>> ws.send('欢迎光临我的博客')
- 30
- >>> ws.recv()
- '\xe6\xac\xa2\xe8\xbf\x8e\xe5\x85\x89\xe4\xb8\xb4\xe6\x88\x91\xe7\x9a\x84\xe5\x8d\x9a\xe5\xae\xa2'
send("内容") :是发送数据给websocket服务端
recv() : 是从websocket服务端获取数据
基于python的websocket开发,tomcat日志web页面实时打印监控案例的更多相关文章
- 如何优雅的关闭基于Spring Boot 内嵌 Tomcat 的 Web 应用
背景 最近在搞云化项目的启动脚本,觉得以往kill方式关闭服务项目太粗暴了,这种kill关闭应用的方式会让当前应用将所有处理中的请求丢弃,响应失败.这种形式的响应失败在处理重要业务逻辑中是要极力避免的 ...
- Python 全栈开发九 日志模块
日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件也有重要性的概念 ...
- python 实现过滤出tomcat日志中含有ERROR 或Exception 的行并保存在另一个文件
遍历多个tomcat日志文件,找出含有ERROR 和Exception 的日志,并把该行日志输出到另一个文件中:(这里为了体现python模块导入的知识,所有建立了多个文件夹和模块) 项目结构: co ...
- web页面内容打印总结
web页面打印有两种,一种是直接调用window.print()命令操作,一种是使用ActiveX插件(Object标签)操作,但是第二种只支持IE内核的浏览器. 示例1: <!DOCTYPE ...
- 基于Python的Webservice开发(一)-简介
之前为了解决Webservice的开发,直接用Python自带的CGI模块挂在IIS上. 但是该方式开发Soap的接口,需要大量的开发,而且安全方面也存在很多问题. 我推荐关于用Python开发Web ...
- python 全栈开发,Day66(web应用,http协议简介,web框架)
一.web应用 web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C/S是客户端 ...
- JAVA WEB学习笔记(三):简单的基于Tomcat的Web页面
注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...
- 基于Python的Webservice开发(三)-Django安装配置
一.安装Django pip install django 二.创建项目 进入指定的目录后 django-admin startproject WebApi 目录说明: WebApi 项目的容器. m ...
- 基于Python的Webservice开发(二)-如何用Spyne开发Webservice
一.功能需求 本次案例是开发一个Item的新建的WebService.IN&OUT的类型JsonDocument. 通过传入相关的参数创建Item,且相关的参数可以被缺省. 二.实现代码 引入 ...
随机推荐
- CentOS添加明细路由
网络架构图 根据最近为客户设计的网络架构,简单的梳理一个网路架构图,当然实际上的网络架构要比这个架构图复杂的多,咱们这边只是用这个图作为一个简单的示例. 拓扑分析 我们要实现专线两端不同网段的 ...
- JavaScript在IE和Firefox的不兼容问题解决方法总结
1.兼容Firefox的 outerHTML,Firefox中没有outerHtml的方法. if (window.HTMLElement) { HTMLElement.prototype.__def ...
- 你可能不知道的github语法——图标
概述 逛github,看到别人的仓库的description里面有各种炫丽的图标,不禁有点好奇,于是去查了下怎么写真的被我查到了,记录在下面,供以后开发时参考,相信对其他人也有用. 图标 可以先看看g ...
- centos7.2 安装 mysql5.7
一.MySQL 5.7 主要特性: 原生支持 Systemd 更好的性能:对于多核 CPU.固态硬盘.锁有着更好的优化更好的 InnoDB 存储引擎 更为健壮的复制功能:复制带来了数据完全不丢失的方案 ...
- 声反馈抑制使用matlab/simulink仿真
第一份工作时做啸叫抑制的仿真,调大0.3可以有大的啸叫产生,下图的SIMULINK仿真模型 实现移相有多种方法: 1.iir实现 2.FFT实现 3.使用FIR实现 所有信号均可以由正弦信号叠加而成.
- 华硕FX503V 安装ubuntu遇到问题解决
关机进bios,确保secure boot是关闭的,第一启动项设为带有uefi 的U盘,也就是ubuntu安装盘,F10保存退出; 接下来会进入grub界面,选择install ubuntu,不要按e ...
- Python常用模块—— Colorama模块
简介 Python的Colorama模块,可以跨多终端,显示字体不同的颜色和背景,只需要导入colorama模块即可,不用再每次都像linux一样指定颜色. 1. 安装colorama模块 pip i ...
- 利用history.pushState()实现页面无刷新更新
本来是在研究vue-router如何记录滚动位置,点返回的时候还是回到原来的位置,看到有人说的history.state存了一个值,才把history研究一下,发现 history.pushState ...
- oracle查看当前用户,数据库实例
#sysdba用户登录[oracle@oracle ~]$ sqlplus / as sysdba #查看当前用户sql>show user; #查看当前数据库实例sql>show par ...
- k8s总结(脑图图片)