invisibleroads / socketIO-client

A socket.io client library for Python
http://pypi.python.org/pypi/socketIO-client
MIT License
447 stars 205 forks source link

socket.io 2.x working PoC #188

Open dpanic opened 6 years ago

dpanic commented 6 years ago

Hi,

I have tried following:

  1. Server NodeJS socket.io 2.0.3 and 2.1.1 (latest)
  2. Python2 and Python3 socket_io_nexus versions from release, master version, and version from pypi

I tried Socket.IO by HTTP server and without. I tried transport channels 'pooling', 'websocket'

I always have same error: 400 {"code":1,"message":"Session ID unknown"}

So please can somebody share working PoC?

maxmedina05 commented 6 years ago

wait what? are you using socket_io_nexus? if so, then can you share your code? I got it working with NodeJS as socket.io (2.x) server and python running nexus as client. I didn't do anything weird.

dpanic commented 6 years ago
  1. Yes of course I am using socket_io_nexus
  2. I also use MacOS (tested python2 and python3)
  3. Tested to download socket_io_nexus inside directory (plus requirements) and install by pip

Here is code which is not working:

Python code:

import os
import sys
import time

__DIR__ = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, __DIR__ + "/modules/")

from socketIO_client_nexus import SocketIO, LoggingNamespace

ref_socket = SocketIO('http://127.0.0.1', 45623, LoggingNamespace, verify=False)

error from socketIO_client_nexus installed by PIP:

 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/socketIO_client_nexus/transports.py", line 186, in get_response
    status_code, response.text))
socketIO_client_nexus.exceptions.ConnectionError: unexpected status code (400 {"code":1,"message":"Session ID unknown"})

error if I download from here https://github.com/nexus-devs/socketIO-client-2.0.3:

  File "/Users/dpanic/Desktop/project/dependencies/socketIO_client_nexus/transports.py", line 186, in get_response
    status_code, response.text))
socketIO_client_nexus.exceptions.ConnectionError: unexpected status code (400 {"code":1,"message":"Session ID unknown"})
maxmedina05 commented 6 years ago

I copied and pasted the code and I don't the error. Although, I'm using windows. are you doing something else?

dpanic commented 6 years ago

I am using MacOS. I will try on Linux tomorrow. However I don't think that should be the issue.

Can you please share your NodeJS code?

P.S. If i set transports: ['websocket'] Python can't connect, it's stucked. So only 'pooling' is working

maxmedina05 commented 6 years ago

Here I'm running socket io and expressjs at the same time.

https://pastebin.com/XNVeVms5

dpanic commented 6 years ago

My code is similar:

const http = require('http');
const io = require('socket.io');

let options  = {
        pingInterval: 10 * 1000,
        pingTimeout: 5 * 1000,
        //transports: [ 'websocket' ],
};

let server = http.createServer();
let ref_socket_server = io.listen(server, options);

ref_socket_server.listen(45623, 127.0.0.1, function () {});
jupe commented 6 years ago

I’ve also similar problems when using socket.io 2.x. Have you tried https with socket.io 2.x ?

dpanic commented 6 years ago

no i haven't. i don't think it will change anything. but i will try.

xxxenobyte commented 6 years ago

i have a same problem. Client: python (socketIO-client-nexus 0.7.6); Server: nodejs ("socket.io": "^2.0.3").

client:

from socketIO_client_nexus import SocketIO, LoggingNamespace, BaseNamespace
from requests.exceptions import ConnectionError

def on_connect():
    print('[Connected]')

def on_reconnect():
    print('[Reconnected]')

def on_disconnect():
    print('[Disconnected]')

def on_login(*args):
    print('login ', args)

def on_connect_error():
    print('connect_error')

def on_error():
    print('socket error')

socket = SocketIO(
    'https://some.domen.com', 443,
    params={"email": "email@email.com", "passwords": "pass"}
    )

socket.on('connect', on_connect)
socket.on('login', on_login)
socket.on('reconnect', on_reconnect)
socket.on('disconnect', on_disconnect)
socket.wait()

Are there any other libraries for socketio-client?

maxmedina05 commented 6 years ago

btw, have you tried using another port? in that example, you are using 443 (https) for an http address

xxxenobyte commented 6 years ago

@maxmedina05 sorry, wrong address. I'm using this address: https/some.domen.com But somehow still does not respond to the event "login"

dpanic commented 6 years ago

Maybe I am repeating myself, but when in NodeJS Socket.IO transports: [ 'pooling' ] than it starts and makes error which I mentioned. If I set transports: [ 'websocket' ] than it's stucked completelly.

This looks to me problem is there, it 'must' go over 'websocket' than problem will be solved

xxxenobyte commented 6 years ago

What's wrong: WARNING:socketIO-client:some.domen.com:443/socket.io [connection error] recv disconnected by SSL ([SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2273)) ?

xxxenobyte commented 6 years ago

@dpanic turn on logs and show what gives out

logging.getLogger('socketIO-client-nexus').setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
dpanic commented 6 years ago

Python3, pooling

DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.86.253
DEBUG:requests.packages.urllib3.connectionpool:http://192.168.86.253:45623 "GET /socket.io/?EIO=3&transport=polling&t=1529569781045-0 HTTP/1.1" 200 100
Traceback (most recent call last):
  File "testsocket.py", line 34, in <module>
    ref_socket = SocketIO('http://192.168.86.253', 45623, LoggingNamespace, verify=False)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/__init__.py", line 353, in __init__
    resource, hurry_interval_in_seconds, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/__init__.py", line 54, in __init__
    self._transport
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/__init__.py", line 62, in _transport
    self._engineIO_session = self._get_engineIO_session()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/__init__.py", line 76, in _get_engineIO_session
    transport.recv_packet())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/transports.py", line 83, in recv_packet
    for engineIO_packet in decode_engineIO_content(response.content):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/parsers.py", line 47, in decode_engineIO_content
    content, content_index)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socketIO_client_nexus/parsers.py", line 125, in _read_packet_length
    while content.decode()[content_index] != ':':
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 3: invalid start byte
DEBUG:socketIO-client:192.168.86.253:45623/socket.io [socket.io disconnect]
dpanic commented 6 years ago

Python3, websocket

DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.86.253
DEBUG:requests.packages.urllib3.connectionpool:http://192.168.86.253:45623 "GET /socket.io/?EIO=3&transport=polling&t=1529569862759-0 HTTP/1.1" 400 None
WARNING:socketIO-client:192.168.86.253:45623/socket.io [engine.io waiting for connection] unexpected status code (400 {"code":0,"message":"Transport unknown"})
DEBUG:requests.packages.urllib3.connectionpool:http://192.168.86.253:45623 "GET /socket.io/?EIO=3&transport=polling&t=1529569864025-0 HTTP/1.1" 400 None
DEBUG:requests.packages.urllib3.connectionpool:http://192.168.86.253:45623 "GET /socket.io/?EIO=3&transport=polling&t=1529569865034-0 HTTP/1.1" 400 None
DEBUG:requests.packages.urllib3.connectionpool:http://192.168.86.253:45623 "GET /socket.io/?EIO=3&transport=polling&t=1529569866044-0 HTTP/1.1" 400 None
bepehr commented 6 years ago

Any news about this error ? i got same error too

jupe commented 6 years ago

We have been investigating this a bit and looks that we have ~working solution quite close in fork: https://github.com/ARMmbed/socketIO-client/pull/6 - but all kind of help would be more than welcome..

dpanic commented 6 years ago

I'll test and let you know

dpanic commented 6 years ago

Hey @jupe . I have tested and here is what I know after testing.

Python2 and Python3 with branch socketio-2.0 works properly ONLY if server is started in single process mode. Archive.zip

In case you start it as a cluster, it is NOT working!

I uploaded test ZIP folder.

dpanic commented 6 years ago

If I start cluster with 2 workers. Than all works. 3,4,5...sometimes works, sometimes not. So this is race condition somewhere in NodeJS http/https and/or socket.io, now question is where.

jupe commented 6 years ago

🤔 have you tried socket.io 1.x (for example head of master) with cluster?

dpanic commented 6 years ago

@jupe yes. I use it on production server for 1+ year...all works good

dpanic commented 6 years ago

@jupe should we pass information to guys who made fork about cluster/non-cluster mode?

jupe commented 6 years ago

I did not be able to verify that socket.io@1.x server with cluster example you provided work with our master branch (without sio 2 changes).Same failure happens (Session ID unknown). Doesn't it need to use some sticky approach to get clusters working with socket.io server (https://socket.io/docs/using-multiple-nodes/) ?

jupe commented 6 years ago

properly configured cluster with Sticky load balancing seems to work fine with ARMmbed#6 for me even with socket.io@2.1.1 node server.

dpanic commented 6 years ago

Sticky load balancing is using 'websockets' as transport, nothing more.

This is error:

WARNING:socketIO-client:127.0.0.1:18765/socket.io [engine.io waiting for connection] unexpected status code (400 {"code":0,"message":"Transport unknown"})

What is meaning of "properly configured cluster" ?

dpanic commented 6 years ago

So, here is the thing. If I select sticky routing in Socket.IO on server side, NodeJS side. Than Python client side is sending for some reason 'XHR-POOLING'.

  1. If it is single process, than it connects like this: EBUG:urllib3.connectionpool:http://127.0.0.1:18765 "GET /socket.io/?EIO=3&transport=polling&t=1536571950010-0 HTTP/1.1" 200 103 DEBUG:socketIO-client:127.0.0.1:18765/socket.io [engine.io transport selected] websocket

  2. if it is > 1 than like this: DEBUG:urllib3.connectionpool:http://127.0.0.1:18765 "GET /socket.io/?EIO=3&transport=polling&t=1536571976712-0 HTTP/1.1" 200 103 DEBUG:socketIO-client:127.0.0.1:18765/socket.io [engine.io transport selected] xhr-polling

Does anybody knows anything about this?