ws-element/app.py

78 lines
1.7 KiB
Python

import time
import json
from random import choice, random
from flask import Flask, render_template
from flask_sock import Sock
app = Flask(__name__)
sock = Sock(app)
app.debug = True
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
def strftime():
return time.strftime('%H:%m:%S')
@app.route('/')
def index():
return render_template('index.html')
@sock.route('/time')
def route_time(sock):
while True:
sock.send(strftime())
time.sleep(1)
@sock.route('/logs')
def route_logs(sock):
id = 0;
sock.send(json.dumps({
'id': 'Id',
'time': 'Time',
'value': 'Value',
'message': 'Message'
}))
with open('/usr/share/wordlists/wordlist') as wl:
lines = wl.readlines()
while True:
time.sleep(random() * 3)
message = '{} {} {}'.format(choice(lines), choice(lines), choice(lines))
sock.send(json.dumps({
'id': id,
'time': str(strftime()),
'value': int(random() * 100),
'message': message
}))
id = id + 1
@sock.route('/ticker')
def route_ticker(sock):
tick = 0;
while True:
sock.send(tick);
tick = tick + 1
time.sleep(0.5)
@sock.route('/echo')
def route_echo(sock):
while True:
data = sock.receive()
sock.send(data)