简体   繁体   中英

How to connect to socket.io using python

I want to connect to Socket.IO server using python. Is there a way to do it?

I have tried websocket-client as suggested in this answer .

ws = create_connection("ws://example.com:1000/socket.io/")

That code throws this exception

websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

I feel like I am missing parameters because the JS client connection URL looks like this:

ws://example.com:1000/socket.io/?EIO=3&transport=websocket&sid=CHARSANDNUMBERS

You just need to use the url from the JS client, possibly without the sid:

ws = create_connection("ws://example.com:1000/socket.io/?EIO=3&transport=websocket")

From my personal experience I didn't have that sid param, but just try to add it if it doesn't work without.

You can look at this: socketIO-client

As an example,

from socketIO_client import SocketIO

socketIO = SocketIO('localhost', 8000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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