From Idiotic Parakeet, 2 Months ago, written in Plain Text.
  1. from flask import Flask, render_template, flash, jsonify, request, send_file, url_for
  2. from flask_socketio import SocketIO, emit
  3. from fetch import get_data
  4. import threading
  5. from time import sleep, time
  6.  
  7. app = Flask(__name__)
  8. app.config['SECRET_KEY'] = 'it is a secret'
  9. socketio = SocketIO(app)
  10.  
  11. data = []
  12.  
  13. class Data_thread(threading.Thread):
  14.     def __init__(self):
  15.         super(Data_thread, self).__init__()
  16.         self._stop_event = threading.Event()
  17.  
  18.     def run(self, ar):
  19.         global data
  20.         with app.test_request_context('/'):
  21.             while(1):
  22.                 if(self.stopped):
  23.                     break
  24.                 else:
  25.                     x = 3
  26.                     data_point = {'azimuth': 1, 'elevation': 2, 'x': x, 'y': 4, 'z': 5, 'timestamp': time()*1000}
  27.                     emit('data', data_point, namespace="/test", broadcast=True)
  28.                     data.append(data_point)
  29.                     print("hey")
  30.                     sleep(3)
  31.  
  32.     def stop(self):
  33.         self._stop_event.set()
  34.  
  35.     def stopped(self):
  36.         return self._stop_event.is_set()
  37.  
  38. x = 0
  39. data_t = None
  40.  
  41. @app.route('/')
  42. def homepage():
  43.     return render_template("index.html")
  44.  
  45. @app.route("/downloads/<path:filename>")
  46. def download(filename):
  47.     try:
  48.         return send_file(url_for('storage', filename=str(filename)))
  49.     except Exception as e:
  50.         return(str(e))
  51.  
  52.  
  53. @socketio.on('connect', namespace='/test')
  54. def test_connect():
  55.     #global socketio
  56.     print("fucking connected")
  57.  
  58. @socketio.on("start", namespace="/test")
  59. def start():
  60.     global data_t
  61.     data_t = Data_thread()
  62.     data_t.start()
  63.  
  64. @socketio.on("stop", namespace="/test")
  65. def stop():
  66.     global data_t
  67.     global data
  68.     if not data_t is None:
  69.         data_t.stop()
  70.         data_t.join()
  71.         print("thread exited gracefully")
  72.         print(data)
  73.  
  74. @socketio.on('disconnect', namespace='/test')
  75. def test_disconnect():
  76.     print('Client disconnected')
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     socketio.run(app)
captcha