Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 699

websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400 [closed]

$
0
0

I can't connect to a websocket using websockets module. I have the following code:

import asyncio, sslimport websocketsheaders= {'Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'en-US,en;q=0.9','Cache-Control': 'no-cache','Connection': 'Upgrade','Host': ...,'Origin': ...,'Pragma': 'no-cache','Upgrade': 'websocket','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'    }async def hello():    uri = ...    async with websockets.connect(ssl=0,                                  extra_headers=headers,                                  origin="*",                                  uri = uri) as websocket:        passasyncio.get_event_loop().run_until_complete(hello())

I get the following exception:

Traceback (most recent call last):  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\kr ws.py", line 30, in <module>    asyncio.get_event_loop().run_until_complete(hello())  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete    return future.result()  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\kr ws.py", line 19, in hello    async with websockets.connect(ssl=0,  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\site-packages\websockets\client.py", line 517, in __aenter__    return await self  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\site-packages\websockets\client.py", line 542, in __await_impl__    await protocol.handshake(  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\site-packages\websockets\client.py", line 296, in handshake    raise InvalidStatusCode(status_code)websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400

But the issue is, I can connect to the websocket using websocket module, but I don't want to use it due to several reasons. But if it helps, here is the code using websocket:

import websocket, ssltry:    import threadexcept ImportError:    import _thread as threadimport time, msgpackheaders= {'Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'en-US,en;q=0.9','Cache-Control': 'no-cache','Connection': 'Upgrade','Host': ...,'Origin': ...,'Pragma': 'no-cache','Upgrade': 'websocket','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'    }def on_message(ws, message):    ...def on_error(ws, error):    ...def on_close(ws):    ...def on_open(ws):    ...if __name__ == "__main__":    websocket.enableTrace(True)    ws = websocket.WebSocketApp(...,                              on_message = on_message,                              on_error = on_error,                              on_close = on_close,                              header=headers,                              subprotocols=["binary", "base64"])    ws.on_open = on_open    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

Why can't I connect to the websocket in the first code, while I can connect to same websocket using the other module?

How do I connect using the first one?


Viewing all articles
Browse latest Browse all 699

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>