Hi this issue is related to : how to implement reconnection logic on the client side? #1633
I am collecting data from binance. I am using the following script to download the orderbook every second. I am saving the orderbook data in a log file.
`
from binance import ThreadedWebsocketManager
import logging, time, json
api_key = "api_key"
api_secret = "api_secret"
symbol = "DOGEUSDT"
def main():
logging.basicConfig(
filename=f"example_multiplex_{symbol}_{int(time.time())}.log",
level=logging.INFO,
)
twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
twm.start()
conn_key = None
def restart_socket():
nonlocal conn_key
if conn_key:
twm.stop_socket(conn_key)
conn_key = twm.start_depth_socket(
callback=handle_socket_message,
symbol=symbol,
depth="20",
)
def handle_socket_message(msg):
if msg.get("e") == "error":
logging.warning("Depth stream error: %s", msg)
restart_socket()
return
print(msg)
logging.info(json.dumps(msg))
restart_socket()
twm.join()
if name == "main":
main()`
I am getting BinanceWebsocketUnableToConnect. Does anyone know why I am receiving this error and how to reconnect the websocket after receiving this error? I want the data collection script to run indefinitely and not get BinanceWebsocketUnableToConnect error. Can anyone help?
These are the errors in the log file.
`
import glob
files = glob.glob("/home/ubuntu/Documents/DOGE_data/example_*.log")
files = sorted(files, key = lambda x:int(x.split("/")[-1].split("_")[-1].split(".")[0]))
fname = files[-2]
with open(fname,'r') as f:
data = f.read()
data = data.split('\n')
for i,d in enumerate(data):
if "error" in d:
print(i)
print(d)
`
`
173767
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedOK', 'm': 'received 1001 (going away); then sent 1001 (going away)'}
270410
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
270412
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
270413
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
397727
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
397729
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
397730
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
404046
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
404048
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
404049
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
404184
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
404186
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
404187
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
404196
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketUnableToConnect', 'm': ''}
`
Hi this issue is related to : how to implement reconnection logic on the client side? #1633
I am collecting data from binance. I am using the following script to download the orderbook every second. I am saving the orderbook data in a log file.
`
from binance import ThreadedWebsocketManager
import logging, time, json
api_key = "api_key"
api_secret = "api_secret"
symbol = "DOGEUSDT"
def main():
logging.basicConfig(
filename=f"example_multiplex_{symbol}_{int(time.time())}.log",
level=logging.INFO,
)
if name == "main":
main()`
I am getting BinanceWebsocketUnableToConnect. Does anyone know why I am receiving this error and how to reconnect the websocket after receiving this error? I want the data collection script to run indefinitely and not get BinanceWebsocketUnableToConnect error. Can anyone help?
These are the errors in the log file.
`
import glob
files = glob.glob("/home/ubuntu/Documents/DOGE_data/example_*.log")
files = sorted(files, key = lambda x:int(x.split("/")[-1].split("_")[-1].split(".")[0]))
fname = files[-2]
with open(fname,'r') as f:
data = f.read()
data = data.split('\n')
for i,d in enumerate(data):
if "error" in d:
print(i)
print(d)
`
`
173767
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedOK', 'm': 'received 1001 (going away); then sent 1001 (going away)'}
270410
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
270412
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
270413
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
397727
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
397729
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
397730
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
404046
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
404048
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
404049
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
404184
ERROR:binance.ws.reconnecting_websocket:ConnectionClosedError (sent 1011 (internal error) keepalive ping timeout; no close frame received)
404186
WARNING:root:Depth stream error: {'e': 'error', 'type': 'ConnectionClosedError', 'm': 'sent 1011 (internal error) keepalive ping timeout; no close frame received'}
404187
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketClosed', 'm': 'Connection closed. Reconnecting...'}
404196
WARNING:root:Depth stream error: {'e': 'error', 'type': 'BinanceWebsocketUnableToConnect', 'm': ''}
`