如何使用 python-socketio 连接到 'normal' websocket?

How to connect to 'normal' websocket using python-socketio?

据我了解,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 然后它进入某种无限循环并且永远不会 return.

这是我正在尝试的代码:

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()

Socket.IO 不是 WebSocket 的实现,它是在 HTTP 和 WebSocket 之上实现的不同协议。 Socket.IO 客户端只能连接到 Socket.IO 服务器,反之亦然。 WebSocket 协议与 Socket.IO.

不兼容