'Problems with getting information from Binance via Websocket
Could someone help me with a following problem. I'm trying extract some information from Binance via Websocket, but constantly get the same error. I will really appreciate if someone helps me to find an error in my code.
class Websocket_binance:
def __init__(self):
self.url = 'wss://stream.binancefuture.com'
self.ws = None
self.id=0
thread=threading.Thread(self.ws_connect())
thread.run()
logger.info('Binance websocket was initiated')
def ws_connect(self):
ws.enableTrace(True)
self.ws = ws.WebSocketApp(self.url,on_open=self.on_open,on_message=self.on_message, on_error=self.on_error)
self.ws.run_forever()
def on_error(self,ws,err):
logger.error('the following error %s occured',err)
def on_message(self,ws,msg):
logger.info(f'we"ve got a message{msg}')
json.loads(msg)
def on_open(self,ws):
logger.debug(f'Socket was opened')
self.subsribe_channel()
def subsribe_channel(self):
param = {}
param['method'] = 'SUBSCRIBE'
param['params'] = ['btcusdt@bookTicker']
param['id'] = self.id+1
pprint(param)
self.ws.send(json.dumps(param))
web_s = Websocket_binance()
2022-02-12 13:34:10,057 - [DEBUG] - websocket - (_logging.py).dump(59) - --- request header ---
GET / HTTP/1.1
Upgrade: websocket
Host: stream.binancefuture.com
Origin: http://stream.binancefuture.com
Sec-WebSocket-Key: 3HTznVK6ps3tjtkSjf3rEg==
Sec-WebSocket-Version: 13
Connection: Upgrade
2022-02-12 13:34:10,058 - [DEBUG] - websocket - (_logging.py).dump(60) - GET / HTTP/1.1
Upgrade: websocket
Host: stream.binancefuture.com
Origin: http://stream.binancefuture.com
Sec-WebSocket-Key: 3HTznVK6ps3tjtkSjf3rEg==
Sec-WebSocket-Version: 13
Connection: Upgrade
-----------------------
2022-02-12 13:34:10,058 - [DEBUG] - websocket - (_logging.py).dump(61) - -----------------------
--- response header ---
2022-02-12 13:34:10,058 - [DEBUG] - websocket - (_logging.py).trace(78) - --- response header ---
{'id': 1, 'method': 'SUBSCRIBE', 'params': ['btcusdt@bookTicker']}
HTTP/1.1 101 Switching Protocols
2022-02-12 13:34:10,448 - [DEBUG] - websocket - (_logging.py).trace(78) - HTTP/1.1 101 Switching Protocols
Date: Sat, 12 Feb 2022 10:34:10 GMT
2022-02-12 13:34:10,448 - [DEBUG] - websocket - (_logging.py).trace(78) - Date: Sat, 12 Feb 2022 10:34:10 GMT
Connection: upgrade
2022-02-12 13:34:10,448 - [DEBUG] - websocket - (_logging.py).trace(78) - Connection: upgrade
upgrade: websocket
2022-02-12 13:34:10,448 - [DEBUG] - websocket - (_logging.py).trace(78) - upgrade: websocket
sec-websocket-accept:
2022-02-12 13:34:10,448 - [DEBUG] - websocket - (_logging.py).trace(78) - sec-websocket-accept:
-----------------------
2022-02-12 13:34:10,448 - [DEBUG] - websocket - (_logging.py).trace(78) - -----------------------
2022-02-12 13:34:10,448 - [DEBUG] - root - (WebSocket.py).on_open(68) - Socket was opened
++Sent raw: b'\x81\xc2\x83\xf7N\x84\xf8\xd5#\xe1\xf7\x9f!\xe0\xa1\xcdn\xa6\xd0\xa2\x0c\xd7\xc0\xa5\x07\xc6\xc6\xd5b\xa4\xa1\x87/\xf6\xe2\x9a=\xa6\xb9\xd7\x15\xa6\xe1\x83-\xf1\xf0\x93:\xc4\xe1\x98!\xef\xd7\x9e-\xef\xe6\x85l\xd9\xaf\xd7l\xed\xe7\xd5t\xa4\xb2\x8a'
2022-02-12 13:34:10,449 - [DEBUG] - websocket - (_logging.py).trace(78) - ++Sent raw: b'\x81\xc2\x83\xf7N\x84\xf8\xd5#\xe1\xf7\x9f!\xe0\xa1\xcdn\xa6\xd0\xa2\x0c\xd7\xc0\xa5\x07\xc6\xc6\xd5b\xa4\xa1\x87/\xf6\xe2\x9a=\xa6\xb9\xd7\x15\xa6\xe1\x83-\xf1\xf0\x93:\xc4\xe1\x98!\xef\xd7\x9e-\xef\xe6\x85l\xd9\xaf\xd7l\xed\xe7\xd5t\xa4\xb2\x8a'
++Sent decoded: fin=1 opcode=1 data=b'{"method": "SUBSCRIBE", "params": ["btcusdt@bookTicker"], "id": 1}'
2022-02-12 13:34:10,449 - [DEBUG] - websocket - (_logging.py).trace(78) - ++Sent decoded: fin=1 opcode=1 data=b'{"method": "SUBSCRIBE", "params": ["btcusdt@bookTicker"], "id": 1}'
2022-02-12 13:34:10,449 - [ERROR] - root - (WebSocket.py).on_error(61) - the following error Connection to remote host was lost. occured
2022-02-12 13:34:10,451 - [INFO] - root - (WebSocket.py).__init__(52) - Binance websocket was initiated
I just cannot understand why I keep getting the error "Connection to remote host was lost" Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
