Web Socket Receiver: websocket.py
Related dependencies
# pip Install bottle gevent gevent-websocket argparse
frombottle Import request, bottle, abort fromgeventwebsocket Import Websocketerror fromGevent.pywsgi Import Wsgiserver fromGeventwebsocket.handler Import Websockethandlerapp=Bottle () Users=Set() @app.Get('/websocket/') def handle_websocket (): Wsock= Request.environ.Get('Wsgi.websocket') Users.add (wsock)ifNot Wsock:abort ( -,'expected WebSocket request.') whileTrue:Try: Message=wsock.receive () except Websocketerror: BreakPrint U"user:%s"%(len (users))ifmessage: forUserinchUsers:Try: user.send (message) except Websocketerror:print u'Kill'users.remove (wsock) Server= Wsgiserver (("0.0.0.0",1019), app,handler_class=Websockethandler) server.serve_forever ()
Service side: logs.py
Related dependencies:
Pip Install Websocket-client
Import subprocess fromwebsocket Import Create_connectionws_server="ws://172.18.30.19:1010/websocket/"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) whileTrue:line=popen.stdout.readline (). Strip () Ws.send (line)
Application Front End: logs.html
<! DOCTYPE html>html, body {font:normal0. 9em Arial, Helvetica; } #log {width:440px; height:200px; border:1px solid #7F9DB9; Overflow:auto; } #msg {width:330px; } </style> <script>varsocket; function init () {varHost ="ws://172.18.30.19:1010/websocket/"; Try{Socket=NewWebSocket (host); Socket.onopen=function (msg) {log ('Connected'); }; Socket.onmessage=function (msg) {log (msg.data); }; Socket.onclose=function (msg) {log ("lose connection!"); }; } Catch(ex) {log (ex); } $("msg"). focus (); } function Send () {vartxt, 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) {returndocument.getElementById (ID); } function log (msg) {$ ("Log"). InnerHTML + ="<br>"+msg; } function OnKey (Event) { if(Event. keycode = = -) {send (); } } </script>"Init ()">"Log"></div><input id="msg"Type="textbox"onkeypress="OnKey (Event)"/><button onclick="Send ()"> Send </button></body>Test a piece:
Conclusion:
Front End--"receiver"--service side
Based on demand use case, case&coding two line parallel, service (M) & Consumption (VC) separation, unit, interface, function, integrated four layer quality management, automation integration, testing, delivery support. 3 Phases (Requirement analysis stage, research and development preparation stage, research and development test stage) 16 small Course (*) Determine the boundary, clear the product, and do a good job in service management.
Python-based WebSocket development, Tomcat Log Web page real-time print monitoring case