Python Forum

Full Version: Can't connect to websocket
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I've spent some time now trying to connect to this websocket and I tried at least five different socket libraries trying to connect with no joy.  Think I'm using Python 3.6

Depending on the library I ended up with either this:

This code:

import websocket


def on_message(ws, message):
   print("Message: " + message)


def on_error(ws, error):
   print("Error: " + error)


def on_close(ws, close):
   print(close)


if __name__ == "__main__":

   websocket.enableTrace(True)
   ws = websocket.WebSocketApp("wss://streamer.cryptocompare.com",
                               on_message=on_message,
                               on_error=on_error,
                               on_close=on_close)

   ws.send("[2~Poloniex~BTC~USD]")
   result = ws.recv()
   print("Received '%s'" % result)
Produced:

Error:
File "C:\Program Files\Python36\lib\site-packages\websocket\_app.py", line 120, in send    "Connection is already closed.") websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
While this:

import asyncio
import websockets

async def hello():
   async with websockets.connect('wss://streamer.cryptocompare.com') as websocket:
       name = input("[0~Poloniex~BTC~USD]")
       await websocket.send(name)
       print("> {}".format(name))

       greeting = await websocket.recv()
       print("< {}".format(greeting))

asyncio.get_event_loop().run_until_complete(hello())
Came up with this error:

Error:
File "C:\Program Files\Python36\lib\site-packages\websockets\client.py", line 81, in handshake raise InvalidHandshake("Malformed HTTP message") from exc websockets.exceptions.InvalidHandshake: Malformed HTTP message
In the case of the second error I tried changing the format of the message, converting it to json, removing square brackets, etc. without success.

Is there someone who could give me some guidance on connecting to the websocket please?

Server side runs the socket on socket.io.

Any help is greatly appreciated!

Thank you all!
I found a solution to my problem. The websocket in question is no longer online. Big Grin
Hi Iaas

Could you please let me know how you found that wss://streamer.cryptocompare.com is no longer online? The crypto compare website docs still seem to refer to this.

Thanks