通过 python-socketio 发送数据但无法在 flask socketio 上接收数据
Sending data through python-socketio but unable to receive data on flask socketio
我正在尝试从客户端发送数据,我正在使用 python-socketio 作为下面的 mentioend
#Client.py
import time
import socketio
sio = socketio.Client(engineio_logger=True)
start_timer = None
# if __name__ == '__main__':
sio.connect('http://127.0.0.1:3000')
sio.wait()
sio.emit('connect', {"Data": "Device_id"})
并尝试在使用下面代码中提到的 flask-socketio 的服务器上运行
#Server.py
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
# app.config['SECRET_KEY'] = "Social Distance Secret"
socket_app = SocketIO(app)
@socket_app.on('connect')
def handle_id(data):
print(data)
print(request.sid)
if __name__ == '__main__':
socket_app.run(app, debug=True, host='127.0.0.1', port=3000)
我能够接收到 sid,但在发送到 WebSocket
时无法获取我在 client.py 中给出的参数
# Error Server.py
Traceback (most recent call last):
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\engineio\server.py", line 545, in _trigger_event
return self.handlers[event](*args)
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 721, in _handle_eio_connect
return self._handle_connect(sid, '/')
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 626, in _handle_connect
self.environ[sid])
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 708, in _trigger_event
return self.handlers[namespace][event](*args)
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_socketio-4.3.1.dev0-py3.6.egg\flask_socketio\__init__.py", line 283, in _handler
*args)
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_socketio-4.3.1.dev0-py3.6.egg\flask_socketio\__init__.py", line 711, in _handle_event
ret = handler()
TypeError: handle_id() missing 1 required positional argument: 'data'
# Errors client.py:
Attempting polling connection to http://127.0.0.1:3000/socket.io/?transport=polling&EIO=3
Traceback (most recent call last):
File "C:/Users/varul.jain/Desktop/people_counter/listenn.py", line 12, in <module>
sio.connect('http://127.0.0.1:3000')
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\client.py", line 279, in connect
six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Unexpected status code 401 in server response
任何建议都会很有帮助
第一个问题是您正在使用 connect
事件,但该事件已被保留。将事件名称更改为其他名称:
@socket_app.on('connected')
def handle_id(data):
print(data)
print(request.sid)
第二个问题是,在您的客户端中您正在调用 wait()
,它会阻塞直到连接结束,因此您的 emit()
调用永远不会到达 运行。在等待之前发送发射:
sio.connect('http://127.0.0.1:3000')
sio.emit('connected', {"Data": "Device_id"})
sio.wait()
我正在尝试从客户端发送数据,我正在使用 python-socketio 作为下面的 mentioend
#Client.py
import time
import socketio
sio = socketio.Client(engineio_logger=True)
start_timer = None
# if __name__ == '__main__':
sio.connect('http://127.0.0.1:3000')
sio.wait()
sio.emit('connect', {"Data": "Device_id"})
并尝试在使用下面代码中提到的 flask-socketio 的服务器上运行
#Server.py
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
# app.config['SECRET_KEY'] = "Social Distance Secret"
socket_app = SocketIO(app)
@socket_app.on('connect')
def handle_id(data):
print(data)
print(request.sid)
if __name__ == '__main__':
socket_app.run(app, debug=True, host='127.0.0.1', port=3000)
我能够接收到 sid,但在发送到 WebSocket
时无法获取我在 client.py 中给出的参数# Error Server.py
Traceback (most recent call last):
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\engineio\server.py", line 545, in _trigger_event
return self.handlers[event](*args)
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 721, in _handle_eio_connect
return self._handle_connect(sid, '/')
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 626, in _handle_connect
self.environ[sid])
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\server.py", line 708, in _trigger_event
return self.handlers[namespace][event](*args)
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_socketio-4.3.1.dev0-py3.6.egg\flask_socketio\__init__.py", line 283, in _handler
*args)
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_socketio-4.3.1.dev0-py3.6.egg\flask_socketio\__init__.py", line 711, in _handle_event
ret = handler()
TypeError: handle_id() missing 1 required positional argument: 'data'
# Errors client.py:
Attempting polling connection to http://127.0.0.1:3000/socket.io/?transport=polling&EIO=3
Traceback (most recent call last):
File "C:/Users/varul.jain/Desktop/people_counter/listenn.py", line 12, in <module>
sio.connect('http://127.0.0.1:3000')
File "C:\Users\varul.jain\AppData\Local\Programs\Python\Python36\lib\site-packages\socketio\client.py", line 279, in connect
six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Unexpected status code 401 in server response
任何建议都会很有帮助
第一个问题是您正在使用 connect
事件,但该事件已被保留。将事件名称更改为其他名称:
@socket_app.on('connected')
def handle_id(data):
print(data)
print(request.sid)
第二个问题是,在您的客户端中您正在调用 wait()
,它会阻塞直到连接结束,因此您的 emit()
调用永远不会到达 运行。在等待之前发送发射:
sio.connect('http://127.0.0.1:3000')
sio.emit('connected', {"Data": "Device_id"})
sio.wait()