Python tornado websocket real-time log display, tornadowebsocket

Source: Internet
Author: User

Python tornado websocket real-time log display, tornadowebsocket

I,Topic: Displays dynamically generated log files on the server in real time.

II,Process:

1. The client browser establishes a websocket link with the server. The server suspends the instance to save the link and waits for the new content to trigger the return action.

2. log server scripts cyclically discover new content and discover new content to tornado waiting for API POST

3. the tornado processor receives new content and returns a new line to the saved client link through websocket.

4. The client browser receives, modifies, and displays new content.

III,Code:

1. tornado server url routing, basic page handler, and html:

# The module Path depends on your project (R'/logs/index/', 'apps. logs. handler. indexHandler '), (R'/logs/newlinesforcallbacker/', 'apps. logs. handler. writeNewLinesHandler '), (R'/logs/newlinesforserver/', 'apps. logs. handler. receiveNewLinesHandler '),
Class IndexHandler (BaseHandler): '''homepage ''' def get (self, * args, ** kwargs): self. render ('logs/index.html ')
<Div align = "center"> 

 

2. Server websocket handler (WriteNewLinesHandler), save and delete link processing class, and client websocket request js

Class ProStatus (object): ''' processing class ''' w_register = [] def register (self, callbacker): ''' records client connection instance ''' self. w_register.append (callbacker) def unregister (self, callbacker): '''delete client connection instance' ''' self. w_register.remove (callbacker) def makelines (self, lines): '''process accepted row content ''' pass def trigger (self, line ): ''' send the latest content to all recorded clients ''' passclass WriteNewLinesHandler (tornado. websocket. webSocketHandler): ''' accept websocket links and save the linked instance ''' def check_origin (self, origin ): # return True def open (self): ''' process new connection ''' ProStatus () for websocket processing class rewrite same-source check (). register (self) def on_close (self): ProStatus (). unregister (self) # Delete client connection def on_message (self, message): pass
$ (Function () {function requestText () {host = "ws: //" + location. hostname + ":" + location. port + "/logs/newlinesforcallbacker/" websocket = new WebSocket (host) websocket. onopen = function (evt) {}// establish a websocket connection. onmessage = function (evt) {// obtain the information returned by the server data = $. parseJSON (evt. data) $ ("# main "). append (data + "</br>") // write to the page} websocket. onerror = function (evt) {}} requestText ()})

 

3. tornado waits for the script to submit new content processing handler (javasenewlineshandler), sends new content functions to the pending client, and traverses the log to submit new content scripts

Class ProStatus (object): ''' processing class ''' w_register = [] def register (self, callbacker ): '''record client connection instance' ''pass def unregister (self, callbacker): '''delete client connection instance' ''pass def makelines (self, lines ): '''processing accepted row content ''' for line in lines: self. trigger (line) def trigger (self, line): ''' sends the latest content to all recorded clients ''' for callabler in self. w_register: callabler. write_message (json. dumps (line) class encode enewlineshandler (BaseHandler): ''' receives the latest line content submitted by the server script ''' def post (self, * args, ** kwargs ): linesdata = self. get_argument ('lines', '') # print type (json. loads (linesdata) ProStatus (). makelines (json. loads (linesdata ))
#-*-Coding: UTF-8-*-_ author _ = 'zhouwang' import timeimport urllibimport urllib2import jsonp = 0 while True: f = open('logs.txt ', 'r + ') f. seek (p, 0) # offset to the last end position line = f. readlines () if lines: # operation on the row content, send the latest row content data = urllib to the server. urlencode ({'lines': json. dumps (lines)}) url = 'HTTP: // localhost: 8800/logs/newlinesforserver/'req = urllib2.Request (url, data) res_data = urllib2.urlopen (req) # print res_data.read () # obtain the current position as the offset value p = f. tell () f. close () time. sleep (1)

 

Ps: traverse the big log file, and filter the line content please refer to: http://www.cnblogs.com/wowoo1121/p/5381971.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.