From Small Dove, 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.         self._stop_event.clear()
  18.  
  19.     def run(self):
  20.         global data
  21.         print("fuck I'm going to run")
  22.         with app.test_request_context('/'):
  23.             while(1):
  24.                 if(not self.stopped):
  25.                     print("I'm breaking up with you mate")
  26.                     break
  27.  
  28.                 else:
  29.                     x = 3
  30.                     print("I'm in  the emit block mate")
  31.                     data_point = {'azimuth': 1, 'elevation': 2, 'x': x, 'y': 4, 'z': 5, 'timestamp': time()*1000}
  32.                     emit('data', {'azimuth': 1, 'elevation': 2, 'x': x, 'y': 4, 'z': 5, 'timestamp': time()*1000}, namespace="/test", broadcast=True)
  33.                     data.append(data_point)
  34.                     print("hey")
  35.                     sleep(3)
  36.  
  37.     def stop(self):
  38.         self._stop_event.set()
  39.  
  40.     def stopped(self):
  41.         return self._stop_event.is_set()
  42.  
  43. x = 0
  44. data_t = None
  45.  
  46. @app.route('/')
  47. def homepage():
  48.     return render_template("index.html")
  49.  
  50. @app.route("/downloads/<path:filename>")
  51. def download(filename):
  52.     try:
  53.         return send_file(url_for('storage', filename=str(filename)))
  54.     except Exception as e:
  55.         return(str(e))
  56.  
  57.  
  58. @socketio.on('connect', namespace='/test')
  59. def test_connect():
  60.     #global socketio
  61.     print("fucking connected")
  62.  
  63. @socketio.on("start", namespace="/test")
  64. def start(msg):
  65.     global data_t
  66.     print("received the start sig")
  67.     data_t = Data_thread()
  68.     data_t.start()
  69.  
  70. @socketio.on("stop", namespace="/test")
  71. def stop(msg):
  72.     global data_t
  73.     global data
  74.     if not data_t is None:
  75.         data_t.stop()
  76.         data_t.join()
  77.         print("thread exited gracefully")
  78.         print(data)
  79.  
  80. @socketio.on('disconnect', namespace='/test')
  81. def test_disconnect():
  82.     print('Client disconnected')
  83.  
  84.  
  85. if __name__ == '__main__':
  86.     socketio.run(app)
  87.  
captcha