-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
101 lines (87 loc) · 2.88 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
from tornado.options import define, options
from tornado.process import Subprocess
define("port", default=7777, help="Run server on a specific port", type=int)
html_template = """
<!DOCTYPE html>
<html>
<head>
<title>tornado WebSocket example</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1>tornado WebSocket example</h1>
<hr>
WebSocket status : <span id="message"></span>
<hr>
<pre>
<div id="content">
</div>
</pre>
</div>
<script>
var ws = new WebSocket('ws://localhost:7777/tail');
var $message = $('#message');
var $content = $('#content');
ws.onopen = function(){
$message.attr("class", 'label label-success');
$message.text('open');
};
ws.onmessage = function(ev){
$message.attr("class", 'label label-info');
$message.hide();
$message.fadeIn("fast");
$message.text('received message');
$content.append(ev.data);
};
ws.onclose = function(ev){
$message.attr("class", 'label label-important');
$message.text('closed');
};
ws.onerror = function(ev){
$message.attr("class", 'label label-warning');
$message.text('error occurred');
};
</script>
</body>
</html>
"""
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write(html_template)
class LogStreamer(tornado.websocket.WebSocketHandler):
def open(self):
filename = "/tmp/simple_foobar.log"
self.proc = Subprocess(["tail", "-f", filename, "-n", "0"],
stdout=Subprocess.STREAM,
bufsize=1)
self.proc.set_exit_callback(self._close)
self.proc.stdout.read_until("\n", self.write_line)
def _close(self, *args, **kwargs):
self.close()
def on_close(self, *args, **kwargs):
logging.info("trying to kill process")
self.proc.proc.terminate()
self.proc.proc.wait()
def write_line(self, data):
logging.info("Returning to client: %s" % data.strip())
self.write_message(data.strip() + "<br/>")
self.proc.stdout.read_until("\n", self.write_line)
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/tail", LogStreamer),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
tornado.options.parse_command_line()
http_server.listen(options.port)
logging.info("TornadoLog started. Point your browser to http://localhost:%d/tail" %
options.port)
tornado.ioloop.IOLoop.instance().start()