Last active
April 16, 2023 02:49
-
-
Save mariocesar/7c4d825fe64957a51c6695e4b5176050 to your computer and use it in GitHub Desktop.
Example Python + Websockets to emulate tail -f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>Tail </title> | |
<style> | |
body {font-family: monospace;} | |
p{ margin: 0;} | |
ul{padding: 0;} | |
li{list-style:none; padding: 0; margin: 0;} | |
</style> | |
</head> | |
<body> | |
<ul id="logger"> | |
</ul> | |
<script> | |
const socket = new WebSocket('ws://localhost:9000'); | |
var log = document.getElementById('logger') | |
// Listen for messages | |
socket.addEventListener('message', function (event) { | |
var text = document.createTextNode(event.data) | |
var li = document.createElement("li") | |
li.appendChild(text) | |
log.prepend(li) | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
We need websockets | |
pip install websockets | |
Run both servers: | |
python tail.py webserver /var/log/syslog | |
python tail.py tailserver /var/log/syslog | |
Go to http://127.0.0.1:8000 | |
Test in linux by: | |
logger "this is a message" | |
""" | |
import time | |
import sys | |
import os | |
import asyncio | |
import websockets | |
from wsgiref.simple_server import make_server | |
loop = asyncio.get_event_loop() | |
options = {} | |
async def tail(websocket, path): | |
with open(options['path'], 'rt') as file: | |
seek = 0 | |
sleep = None | |
while True: | |
file.seek(seek) | |
line = file.readline() | |
where = file.tell() | |
if line: | |
if seek != where: | |
sleep = None | |
await websocket.send(line.strip()) | |
else: | |
sleep = 0.04 | |
seek = where | |
if sleep: | |
time.sleep(sleep) | |
def wsgiapplication(environ, start_response): | |
start_response('200 OK', [('Content-Type', 'text/html')]) | |
return open('index.html', 'rb') | |
def main(action): | |
if action == 'webserver': | |
with make_server('', 8000, wsgiapplication) as httpd: | |
print("Serving on port 8000...") | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
httpd.shutdown() | |
elif action == 'tailserver': | |
tail_server = websockets.serve(tail, '', 9000) | |
loop.run_until_complete(tail_server) | |
loop.run_forever() | |
if __name__ == '__main__': | |
assert len(sys.argv) == 3, "Required at least two arguments, action and log path. | |
_, action, path = sys.argv | |
options['path'] = path | |
main(action) |
in tail.py
line 8 and 9 whats the use of /var/log/syslog
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it shows me key error on here
with open(options['path'], 'rt') as file: