Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. """
  2. Python 3.4.4
  3.  
  4. Flask==0.10.1
  5. Jinja2==2.8
  6. MarkupSafe==0.23
  7. Werkzeug==0.11.3
  8. cffi==1.5.0
  9. cryptography==1.2.2
  10. idna==2.0
  11. itsdangerous==0.24
  12. pyOpenSSL==0.15.1
  13. pyasn1==0.1.9
  14. pycparser==2.14
  15. six==1.10.0
  16. """
  17.  
  18. from datetime import datetime
  19. from time import sleep
  20.  
  21. from flask import Flask, Response, render_template
  22.  
  23. app = Flask(__name__)
  24.  
  25. index_html = """
  26. <!doctype html><html lang=en>
  27. <head>
  28. <title>SSL Stream</title>
  29. </head>
  30. <body>
  31. <h1>hello, world</h1>
  32. <ul id="events"></ul>
  33. <script>
  34. window.onload = function() {
  35. var source = new EventSource("/stream");
  36. source.onmessage = function(e) {
  37.  
  38. var newElement = document.createElement("li");
  39. newElement.innerHTML = e.data;
  40.  
  41. document.getElementById("events").appendChild(newElement);
  42. }
  43. }
  44. </script>
  45. </body>
  46. </html>
  47. """
  48.  
  49. def streamer():
  50. while True:
  51. sleep(1)
  52. data = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
  53. yield 'data: {}\n\n'.format(data)
  54.  
  55. @app.route('/stream')
  56. def stream():
  57. return Response(streamer(), mimetype="text/event-stream")
  58.  
  59. @app.route('/')
  60. def index():
  61. return index_html
  62.  
  63. if __name__ == '__main__':
  64. app.run(debug=True, threaded=True, ssl_context='adhoc')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement