基于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,且相关的参数可以被缺省. 二.实现代码 引入 ...
随机推荐
- hdu3001Travelling
参考了别人的代码 https://blog.csdn.net/u010372095/article/details/38474721 深感自己的弱小 这是tsp问题,和基本的tsp问题没什么大的区 ...
- Winform嵌入CEF(非正常用法)
(一)复制下面必备文件|-- locales|--en-US.pak|--cef.pak|--cef_100_percent.pak |--cef_200_percent.pak|--cef_exte ...
- Scanner 类的使用
/* Scanner类的使用: 1.导类 2.创建对象 3.调用对应的方法获取数据 */ import java.util.Scanner;//导入Scanner类 public class IfDe ...
- C++ lamba使用
Moderm Effective C++ 条款31 第206提到了按引用捕获局部变量和函数形参时,如果lambda式的生命期依赖于局部变量和函数形参的生命期,需注意空悬引用的问题. 原书的例子不够直观 ...
- 通过用jQuery写一个页面,我学到了什么
概述 前几天面试,hr发来一个测试文件,让我做做看.我一看,其实就是根据PSD需求写一个页面,再加上一些互动效果即可. 刚好我之前学了切图,jquery等知识还没练手过,于是高兴的答应了. 最后花了3 ...
- Shell-8--数值运算及处理
RANDOM 默认范围是 0~32767
- location匹配
=/ 表示精确匹配 www.sensetime.com/ ~ :表示做正则表达式匹配,区分字符大小写 ~* : 表示做正则表达式匹配,不区分大小写 ^~: URI的左半部分匹配,不区分大小写 匹配优 ...
- 利用ngnix解决跨域问题
一,定义 跨域是指从一个域名的网页去请求另一个域名的资源,它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制.跨域的严格一点的定义是:只要 协议,域名,端口有任何一个的不同,就 ...
- centos7配置apache服务
首先写下基本的步骤: 1.环境准备:关闭防火墙(不关闭无法访问,我这里只是简单的配置,实际部署项目应该会有具体的设置),关闭selinux(试过,不关闭也没事,一般都关闭) 配置 ip 2.安装软件包 ...
- windows phone开发-windows azure mobile service使用入门
在使用azure之前,我一直只能做本地app,或者使用第三方提供的api,尽管大多数情况下够用,但是仍不能随心所欲操纵数据,这种感觉不是特别好.于是在azure发布后,我就尝试使用azure来做为个人 ...