-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtail.py
More file actions
executable file
·104 lines (84 loc) · 2.38 KB
/
Copy pathtail.py
File metadata and controls
executable file
·104 lines (84 loc) · 2.38 KB
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
102
103
104
#!/usr/bin/python
# Equivalent of "tail -f" as a webpage using websocket
# Usage: webtail.py PORT FILENAME
# Tested with tornado 2.1
# Thanks to Thomas Pelletier for it's great introduction to tornado+websocket
# http://thomas.pelletier.im/2010/08/websocket-tornado-redis/
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import sys
import os
PORT = int(sys.argv[1])
FILENAME = sys.argv[2]
LISTENERS = []
TEMPLATE = """
<!DOCTYPE>
<html>
<head>
<title>WebTail: %s</title>
</head>
<body>
<div id="file"></div>
<script type="text/javascript" charset="utf-8">
function write_line(l) {
document.getElementById('file').innerHTML += l + '<br/>';
}
if ("MozWebSocket" in window) {
WebSocket = MozWebSocket;
}
if (WebSocket) {
var ws = new WebSocket("ws://%s/tail/");
ws.onopen = function() {};
ws.onmessage = function (evt) {
write_line(evt.data);
};
ws.onclose = function() {};
} else {
alert("WebSocket not supported");
}
</script>
</body>
</html>
"""
tailed_file = open(FILENAME)
tailed_file.seek(os.path.getsize(FILENAME))
def check_file():
where = tailed_file.tell()
line = tailed_file.readline()
if not line:
tailed_file.seek(where)
else:
for element in LISTENERS:
element.write_message(line)
class TailHandler(tornado.websocket.WebSocketHandler):
def open(self):
print "WebSocket open"
LISTENERS.append(self)
def on_message(self, message):
pass
def on_close(self):
print "WebSocket close"
try:
LISTENERS.remove(self)
except:
pass
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write(TEMPLATE % (FILENAME, self.request.host))
application = tornado.web.Application([
(r'/', MainHandler),
(r'/tail/', TailHandler),
])
if __name__ == '__main__':
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(PORT)
tailed_callback = tornado.ioloop.PeriodicCallback(check_file, 10)
tailed_callback.start()
io_loop = tornado.ioloop.IOLoop.instance()
try:
io_loop.start()
except SystemExit, KeyboardInterrupt:
io_loop.stop()
tailed_file.close()