简体   繁体   English

如何使用python-socketio连接到“普通” websocket?

[英]How to connect to 'normal' websocket using python-socketio?

As far as I understood, websocket is protocol and socketio is library that implements this protocol. 据我了解, websocket是协议,而socketio是实现此协议的库。

So I decided to move from python websocket-client to python-socketio as it seems easier to implement behaviors with the decorator @sio.on('subject') . 所以我决定从python websocket-client转移到python-socketio因为用装饰器@sio.on('subject')似乎更容易实现行为。

I was playing with different parameter to the connect method but I am always getting errors. 我在使用与connect方法不同的参数,但是总是出现错误。

Using sio.connect('ws://echo.websocket.org') or sio.connect('http://echo.websocket.org', transports=['websocket']) the error is: 使用sio.connect('ws://echo.websocket.org')sio.connect('http://echo.websocket.org', transports=['websocket']) 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

So looking the log I tried sio.connect('http://echo.websocket.org', transports=['websocket'], socketio_path='') but only prints the log Attempting WebSocket connection to ws://echo.websocket.org//?transport=websocket&EIO=3 and then it gets in some kind of infinity loop and never return. 因此,在查看日志时,我尝试了sio.connect('http://echo.websocket.org', transports=['websocket'], socketio_path='')但仅将Attempting WebSocket connection to ws://echo.websocket.org//?transport=websocket&EIO=3的日志打印Attempting WebSocket connection to ws://echo.websocket.org//?transport=websocket&EIO=3 ,然后进入某种无限循环,再也不会返回。

This is the code I am trying: 这是我正在尝试的代码:

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 is not an implementation of WebSocket, it is a different protocol that is implemented on top of HTTP and WebSocket both. Socket.IO不是WebSocket的实现,它是在HTTP和WebSocket两者之上实现的不同协议。 A Socket.IO client can only connect to a Socket.IO server and viceversa. Socket.IO客户端只能连接到Socket.IO服务器,反之亦然。 The WebSocket protocol is incompatible with Socket.IO. WebSocket协议与Socket.IO不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM