Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't connect to websocket
#1
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!
Reply
#2
I found a solution to my problem. The websocket in question is no longer online. Big Grin
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending data from a websocket client to another server lemon 1 3,518 May-25-2020, 02:26 PM
Last Post: lemon
  Client closing websocket connection - proof of concept rmattos 0 1,909 Jan-16-2019, 03:21 PM
Last Post: rmattos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020