Advertisement
AtEchoOff

Python Socket Starter Code

Jan 7th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. =========== app.py ====================
  2.  
  3. from os import environ
  4. from flask import Flask, render_template
  5. from flask_socketio import SocketIO, emit
  6.  
  7. app = Flask(__name__)
  8. app.config['SECRET_KEY'] = 'secret!'
  9. socketio = SocketIO(app)
  10.  
  11.  
  12. #Code here...
  13.  
  14. if __name__ == '__main__':
  15. HOST = environ.get('SERVER_HOST', 'localhost')
  16. PORT = int(environ.get('SERVER_PORT', '5000'))
  17. socketio.run(app, host=HOST, port=PORT)
  18. ================== index.html ========================
  19.  
  20. <!DOCTYPE html>
  21. <html lang="en">
  22.  
  23. <head>
  24. <meta charset="UTF-8">
  25. <title>SocketIO example</title>
  26. <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  27. <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.0/socket.io.js"></script>
  28. </head>
  29.  
  30. <body>
  31. <h1>WebSockets with Flask</h1>
  32. <h2>Receive:</h2>
  33. <div id="log"></div>
  34. <form id="message" method="POST" action='#'>
  35. <input type="text" name="message_data" id="message_data" placeholder="Message">
  36. <input type="submit" value="send">
  37. </form>
  38. <script src="/static/index.js"></script>
  39. </body>
  40.  
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement