【发布时间】:2019-02-03 17:59:40
【问题描述】:
据我了解,websocket 是协议,socketio 是实现此协议的库。
所以我决定从 python websocket-client 转移到 python-socketio,因为使用装饰器 @sio.on('subject') 实现行为似乎更容易。
我正在使用与 connect 方法不同的参数,但我总是遇到错误。
使用sio.connect('ws://echo.websocket.org') 或sio.connect('http://echo.websocket.org', transports=['websocket']) 的错误是:
Attempting polling connection to http://echo.websocket.org/socket.io/?transport=polling&EIO=3
Traceback (most recent call last):
File "/home/lucas/projects/python/py-websockets/client/test.py", line 6, in <module>
sio.connect('ws://echo.websocket.org')
File "/home/lucas/.virtualenvs/py-websockets/lib/python3.6/site-packages/socketio/client.py", line 210, in connect
six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Unexpected status code 404 in server response
所以查看日志我尝试了sio.connect('http://echo.websocket.org', transports=['websocket'], socketio_path=''),但只打印日志Attempting WebSocket connection to ws://echo.websocket.org//?transport=websocket&EIO=3,然后它进入某种无限循环并且永远不会返回。
这是我正在尝试的代码:
import socketio
sio = socketio.Client(logger=True, engineio_logger=True)
@sio.on('connect')
def on_connect(*args, **kwargs):
print(args, kwargs)
if __name__ == '__main__':
sio.connect('http://echo.websocket.org', transports=['websocket'])
sio.wait()
【问题讨论】:
标签: python python-3.x websocket socket.io python-socketio