2

I am trying to connect to socket.io server and receive messages. But when connecting I get socketio.exceptions.ConnectionError: One or more namespaces failed to connect

Code:

import socketio

sio = socketio.Client()

@sio.event
def connect():
    print('connection established')
    sio.emit('login', {'token': 'token'})

@sio.event
def my_message(data):
    print('message received with ', data)

@sio.event
def disconnect():
    print('disconnected from server')

sio.connect('wss://socket.boticord.top')
sio.wait()

Full error log:

Attempting polling connection to https://socket.boticord.top/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': 'JBtkTO-XTOL-OFo2AAAH', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 5000}
Engine.IO connection established
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to wss://socket.boticord.top/socket.io/?transport=websocket&EIO=4
WebSocket upgrade failed: connection error
Sending polling GET request to https://socket.boticord.top/socket.io/?transport=polling&EIO=4&sid=JBtkTO-XTOL-OFo2AAAH
Unexpected status code 400 in server response, aborting
Waiting for write loop task to end
Sending packet CLOSE data None
Engine.IO connection dropped
Traceback (most recent call last):
  File "c:\Users\Kiril\Documents\senko\bot2\senkobot\main2.py", line 6, in <module>
    sio.connect('wss://socket.boticord.top')
  File "C:\Users\Kiril\AppData\Local\Programs\Python\Python39\lib\site-packages\socketio\client.py", line 338, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
HTTP POST request to https://socket.boticord.top/socket.io/?transport=polling&EIO=4&sid=JBtkTO-XTOL-OFo2AAAH failed with error HTTPSConnectionPool(host='socket.boticord.top', port=443): Read timed out. (read timeout=5).
Connection refused by the server, aborting
Exiting write loop task
Exiting read loop task
7
  • 1
    Do you also own the server, or is this a third party? Have you enabled Socket.IO logs to see more detail of the traffic exchanges between client and server? Commented Aug 7, 2021 at 11:50
  • @Miguel No, I have no access to the server. The problem is definitely on my side, since everything works in node.js. Tell me how you can enable socket.io logs? Commented Aug 7, 2021 at 20:40
  • flask-socketio.readthedocs.io/en/latest/… Commented Aug 7, 2021 at 23:04
  • @Miguel i enabled socket.io logs and got output like this: Attempting WebSocket upgrade to wss://socket.boticord.top/socket.io/?transport=websocket&EIO=4 WebSocket upgrade failed: connection error Sending polling GET request to https://socket.boticord.top/socket.io/?transport=polling&EIO=4&sid=WDdR53fSdPOcwGVzAAAK Unexpected status code 400 in server response, aborting based on this, I believe that the websocket for some reason tried to update, but could not do it. Commented Aug 8, 2021 at 0:40
  • Also in the documentation it is written that the server version of socket.io is 3.0.1 and that the version of the client and server must be the same. But the version is specified for the client in node.js and I don't know how to install this version for python. Commented Aug 8, 2021 at 1:04

4 Answers 4

4

try

sio.connect('wss://socket.boticord.top', wait_timeout = 10)
Sign up to request clarification or add additional context in comments.

2 Comments

Why? Some explanation would be helpful as to why this solves the problem.
this is just increasing the wait_timeout, if it worked for you it means, that took longer time to your client to connect to the server
2

I'm surprised no one got this question right. In the source code, O.P. writes this:

sio.connect('wss://socket.boticord.top')

Socket.IO's docs say, quote:

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

For Websocket connection in Python, I'd recommend websocket-client.

If you know the server uses Socket.IO and not Websockets, simply replace the wss with https:

sio.connect("https://socket.boticord.top")

Comments

0

just tell your connection to not check ssl for example: import aiohttp import socketio

async def main():
    connector = aiohttp.TCPConnector(ssl=False)
    async with aiohttp.ClientSession(connector=connector) as http_session:
        sio = socketio.AsyncClient(http_session=http_session)

        @sio.event
        async def connect():
            print("Connected to server");

        try:
            await sio.connect('https://localhost:7294/WebSocketMessageHub')
            await sio.wait()
        except Exception as e:
            print(f"Failed to connect to the server: {e}")


if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Comments

-1

Try to use 4-5.x version of python-socketio or please contact with support.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.