我有一个简单的flask / socketIO应用程序,如下所示:
server.py
from flask import Flask, request
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/work')
def work():
print('in work')
socketio.emit('work', broadcast=True)
print('after emit')
return 'ok'
@socketio.on('report')
def save_report(data):
print('in report...')
with open('f_', 'w') as f:
f.write(data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
client.py
from flask import Flask
from socketIO_client import SocketIO
import sys
socketio = SocketIO('127.0.0.1', 5000 )
app = Flask(__name__)
def handle_work():
print('slave - work')
socketio.emit('report', {'data': 123})
@app.route('/details')
def details():
return {'details': 'during work'}
if __name__ == '__main__':
k = int(sys.argv[1])
socketio.on('work', handle_work)
app.run(host='0.0.0.0', port=5000+k, debug=True)
我分别使用参数1、2和3执行server.py和3 client.py。
然后执行curl localhost:5000 / work
我希望在客户端上执行handle_work()中的代码,但是他们没有收到服务器发出的事件。
有人可以帮我看看问题是什么吗?