slazarov / python-signalr-client

SignalR client for python based on asyncio.
MIT License
33 stars 22 forks source link

Error when connecting to Asp.net SignalR #10

Open ngunhaSO opened 6 years ago

ngunhaSO commented 6 years ago

I am trying your library with python 3.6.5 However, when I try to connect to my vendor signalr endpoint, I got the following error:

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/signalr_aio/_connection.py", line 36, in start self.transport.start() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/signalr_aio/transports/_transport.py", line 47, in start self.ws_loop.run_until_complete(self.socket(self.ws_loop)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete return future.result() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/signalr_aio/transports/_transport.py", line 59, in socket loop=loop) as self.ws: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/websockets/py35/client.py", line 2, in aenter return await self File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/websockets/py35/client.py", line 19, in __await_impl extra_headers=protocol.extra_headers, File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/websockets/client.py", line 263, in handshake raise InvalidStatusCode(status_code) websockets.exceptions.InvalidStatusCode: Status code not 101: 400

Can you help me with this issue?

I have tried with this library https://github.com/TargetProcess/signalr-client-py and it is able to connect sucessfully. However, the project doesn't seem active recently, so i wanna try different available packages.

slazarov commented 6 years ago

Can you post your code or snippet so I could replicate it?

ngunhaSO commented 6 years ago

I used your example code, and replace it with our vendor signalr endpoint. I also pass in session object into the connection.

from signalr_aio import Connection
from base64 import b64decode
from zlib import decompress, MAX_WBITS
import json
from requests import Session

async def process_message(message):
    deflated_msg = decompress(b64decode(message), -MAX_WBITS)
    return json.loads(deflated_msg.decode())
async def on_debug(**msg):
    if 'R' in msg and type(msg['R']) is not bool:
        decoded_msg = await process_message(msg['R'])
        print(decoded_msg)
async def on_error(msg):
    print(msg)
async def on_message(msg):
    decoded_msg = await process_message(msg[0])
    print(decoded_msg)

def print_all_active_devices(data):
    print('\n Active devices: ', data)

if __name__ == "__main__":
    with Session() as session:
        connection = Connection('<signalR_endpoint>', session)
        hub = connection.register_hub('<hub_name>')

        hub.client.on('getAllActiveDevices', print_all_active_devices)
        hub.server.invoke('getAllActiveDevices')
        connection.start()
ngunhaSO commented 6 years ago

Also, please note that if i use your example signalr endpoint, it works. I believe our vendor use asp.net signalr, not the asp.net core signalr.

slazarov commented 6 years ago

It's related to the session you are passing, you are getting 400 Bad Response https://httpstatuses.com/400

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/websockets/client.py", line 263, in handshake
raise InvalidStatusCode(status_code)
websockets.exceptions.InvalidStatusCode: Status code not 101: 400

Is the endpoint cloudflare protected?

What happens if you dont pass a session object, i.e

connection = Connection('<signalR_endpoint>', session=None)

Also correct you method definition to:

async def print_all_active_devices(data):
    print('\n Active devices: ', data)
ngunhaSO commented 6 years ago

I have changed the print_all_active_devices method, and removed the session from the Connection but I still get the same error. The signal endpoint at the moment is still http not https as your example. Does it need to be https?

TiBarification commented 5 years ago

I have the same problem, status code 400, only http provided.