Python 向服务器发送数据时 Flask Socketio BadNamespaceError
Python Flask Socketio BadNamespaceError while sending data to server
我正在尝试从客户端向服务器发送数据,但出现错误的命名空间错误。
我搜索了错误但没有找到任何解决方案...
谁能指出我做错了什么。
服务器端(app.py)
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
socket_app = SocketIO(app)
@socket_app.on('userdata', namespace='/test')
def username(data):
print(data)
print(request.sid)
if __name__ == '__main__':
socket_app.run(app, debug=True, host='127.0.0.1', port=5000)
客户端(client.py)
import socketio
sio = socketio.Client(engineio_logger=True)
data = {}
data['name'] = 'Mark'
sio.connect('http://127.0.0.1:5000')
sio.emit('userdata', data , namespace='/test')
sio.wait()
错误
Attempting polling connection to http://127.0.0.1:5000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': '1ipsM7udDVMj0WdKAAAA', 'upgrades': ['websocket'], 'pingTimeout': 5000, 'pingInterval': 25000}
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to ws://127.0.0.1:5000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Traceback (most recent call last):
File "client.py", line 41, in <module>
sio.emit('userdata', data , namespace='/test')
File "/home/amit/anaconda3/envs/ANPR/lib/python3.6/site-packages/socketio/client.py", line 329, in emit
namespace + ' is not a connected namespace.')
socketio.exceptions.BadNamespaceError: /test is not a connected namespace.
Received packet NOOP data
flask 和 socketio 版本
python-socketio==5.0.4 python-engineio==4.0.0 Flask==1.0.2
Flask-SocketIO==5.0.1
我通过在服务器中调用 connect
处理程序来实现它。
@socket_app.on('connect', namespace='/test')
def test_connect():
print("connected")
emit('connect_custom', {'data': 'Connected'})
然后发送给客户端的 connect_custom
处理程序。
@sio.on('connect_custom', namespace='/test')
def on_connect(data):
print('client contacted by server')
print(data)
data = {}
data['name'] = 'Mark'
sio.emit('userdata', data, namespace='/test')
sio.connect('http://127.0.0.1:5000')
看起来 OP 解决了他们自己的问题,但是当连接到非默认命名空间时,您可以在客户端连接中指定它:
sio.connect('http://127.0.0.1:5000', namespaces=['/test'])
我正在尝试从客户端向服务器发送数据,但出现错误的命名空间错误。
我搜索了错误但没有找到任何解决方案... 谁能指出我做错了什么。
服务器端(app.py)
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
socket_app = SocketIO(app)
@socket_app.on('userdata', namespace='/test')
def username(data):
print(data)
print(request.sid)
if __name__ == '__main__':
socket_app.run(app, debug=True, host='127.0.0.1', port=5000)
客户端(client.py)
import socketio
sio = socketio.Client(engineio_logger=True)
data = {}
data['name'] = 'Mark'
sio.connect('http://127.0.0.1:5000')
sio.emit('userdata', data , namespace='/test')
sio.wait()
错误
Attempting polling connection to http://127.0.0.1:5000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': '1ipsM7udDVMj0WdKAAAA', 'upgrades': ['websocket'], 'pingTimeout': 5000, 'pingInterval': 25000}
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to ws://127.0.0.1:5000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Traceback (most recent call last):
File "client.py", line 41, in <module>
sio.emit('userdata', data , namespace='/test')
File "/home/amit/anaconda3/envs/ANPR/lib/python3.6/site-packages/socketio/client.py", line 329, in emit
namespace + ' is not a connected namespace.')
socketio.exceptions.BadNamespaceError: /test is not a connected namespace.
Received packet NOOP data
flask 和 socketio 版本
python-socketio==5.0.4 python-engineio==4.0.0 Flask==1.0.2 Flask-SocketIO==5.0.1
我通过在服务器中调用 connect
处理程序来实现它。
@socket_app.on('connect', namespace='/test')
def test_connect():
print("connected")
emit('connect_custom', {'data': 'Connected'})
然后发送给客户端的 connect_custom
处理程序。
@sio.on('connect_custom', namespace='/test')
def on_connect(data):
print('client contacted by server')
print(data)
data = {}
data['name'] = 'Mark'
sio.emit('userdata', data, namespace='/test')
sio.connect('http://127.0.0.1:5000')
看起来 OP 解决了他们自己的问题,但是当连接到非默认命名空间时,您可以在客户端连接中指定它:
sio.connect('http://127.0.0.1:5000', namespaces=['/test'])