/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
flask__websocket/commands__websocket__flask-socketio/main.py
80 строк
2 KB
gil9red
Refactoring
28 июн 2023, 20:27
28 июн 2023, 20:27
38fc8f6
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" import uuid from datetime import datetime from flask import Flask, render_template, session, request from flask_socketio import SocketIO, emit # Set this variable to "threading", "eventlet" or "gevent" to test the # different async modes, or leave it set to None for the application to choose # the best option based on installed packages. async_mode = None app = Flask(__name__) app.config["SECRET_KEY"] = "secret!" socketio = SocketIO(app, async_mode=async_mode) @app.route("/") def index(): return render_template("index.html", async_mode=socketio.async_mode) @socketio.on("my_event", namespace="/test") def test_message(message): session["receive_count"] = session.get("receive_count", 0) + 1 print(message) data = message["data"] if data == "CURRENT_DATE_TIME": response = datetime.now().strftime("%Y-%m-%d_%H%M%S") elif data == "UUID": response = str(uuid.uuid4()) elif data.startswith("CALC"): # TODO: don't use in production! response = str(eval(data[4:])) else: response = data emit( "my_response", {"data": response, "count": session["receive_count"]}, ) @socketio.on("my_ping", namespace="/test") def ping_pong(): emit("my_pong") @socketio.on("connect", namespace="/test") def test_connect(): emit("my_response", {"data": "Connected"}) @socketio.on("disconnect", namespace="/test") def test_disconnect(): print("Client disconnected", request.sid) if __name__ == "__main__": HOST = "127.0.0.1" PORT = 12000 print(f"http://{HOST}:{PORT}") socketio.run( app, host=HOST, port=PORT, )