python-socketio 是 Socket.IO 服务器的 Python 实现。
示例代码:
import socketio import eventlet import eventlet.wsgi from flask import Flask, render_template sio = socketio.Server() app = Flask(__name__) @app.route('/') def index(): """Serve the client-side application.""" return render_template('index.html') @sio.on('connect', namespace='/chat') def connect(sid, environ): print("connect ", sid) @sio.on('chat message', namespace='/chat') def message(sid, data): print("message ", data) sio.emit('reply', room=sid) @sio.on('disconnect', namespace='/chat') def disconnect(sid): print('disconnect ', sid) if __name__ == '__main__': # wrap Flask application with engineio's middleware app = socketio.Middleware(sio, app) # deploy as an eventlet WSGI server eventlet.wsgi.server(eventlet.listen(('', 8000)), app)