Connection to remote host was lost
Issue subject
- This error will appear at random time points, and it does not mean that the program cannot run normally. It may occur after 1 day or even 10 minutes after running.
Lost websocket connection
Exception in thread Thread-1:
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/binance/websocket/binance_socket_manager.py", line 55, in run
self.read_data()
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/binance/websocket/binance_socket_manager.py", line 74, in read_data
raise e
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/binance/websocket/binance_socket_manager.py", line 68, in read_data
op_code, frame = self.ws.recv_data_frame(True)
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_core.py", line 408, in recv_data_frame
frame = self.recv_frame()
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_core.py", line 447, in recv_frame
return self.frame_buffer.recv_frame()
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_abnf.py", line 340, in recv_frame
self.recv_header()
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_abnf.py", line 296, in recv_header
header = self.recv_strict(2)
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_abnf.py", line 375, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_core.py", line 531, in _recv
return recv(self.sock, bufsize)
File "/opt/homebrew/Caskroom/miniforge/base/envs/py10_LiangHua/lib/python3.10/site-packages/websocket/_socket.py", line 124, in recv
raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.
Expected behaviour
run forever
Python Code
from binance.websocket.spot.websocket_stream import SpotWebsocketStreamClient
symbols = ['btcusdt']
def message_handler(_, message):
print(message)
def error_handler(message):
print(message)
def close_handler():
print(message)
# 创建websocket连接
my_client = SpotWebsocketStreamClient(on_message=message_handler,
on_error=error_handler,
on_close=close_handler)
# Subscribe to a single symbol stream
[my_client.agg_trade(symbol=symbol) for symbol in symbols]
Environment
Provide any relevant information about your setup, such as:
- Version of binance-connector-python : 3.3.1
- Python version : 3.10
- Operating system: MacOS 14.0
Hi, I'm having the same issue for few days now.
...
backcall==0.2.0
binance-connector==3.3.1
json5==0.9.14
requests==2.31.0
webencodings==0.5.1
websocket-client==1.6.3
websockets==11.0.3
...
- Python version : 3.10.12
- Operating system Linux
@sunshineinwater and @capsian, there's a solution available to address this issue and re-establish the WebSocket connection. Let me demonstrate one approach for you.
while True:
my_client = None
try:
my_client = SpotWebsocketStreamClient(
on_message=message_handler,
on_error=error_handler,
on_close=close_handler
)
[my_client.agg_trade(symbol=symbol) for symbol in symbols]
while True:
time.sleep(10) # check every ten seconds if the websocket is alive
if not my_client.socket_manager.is_alive():
raise Exception("WebSocket connection is not alive")
# You might want to contemplate the utilization of an additional condition, such as an 'if' statement or a condition variable, if your intention is to terminate the connection once a different condition has been met.
except Exception as e:
my_client.stop()
else:
# other instruction
#.
#.
#.
my_client.stop()
break
@sunshineinwater and @capsian, there's a solution available to address this issue and re-establish the WebSocket connection. Let me demonstrate one approach for you.
while True: my_client = None try: my_client = SpotWebsocketStreamClient( on_message=message_handler, on_error=error_handler, on_close=close_handler ) [my_client.agg_trade(symbol=symbol) for symbol in symbols] while True: time.sleep(10) # check every ten seconds if the websocket is alive if not my_client.socket_manager.is_alive(): raise Exception("WebSocket connection is not alive") # You might want to contemplate the utilization of an additional condition, such as an 'if' statement or a condition variable, if your intention is to terminate the connection once a different condition has been met. except Exception as e: my_client.stop() else: # other instruction #. #. #. my_client.stop() break
thanks a lot , but I don't understand the usage at the end of code: could you please give me some advice ?
else: # other instruction #. #. #. my_client.stop() break
These 'other instructions' are just Python code that needs to be executed before closing the WebSocket. Consider that now there is a new version where you can address this problem by defining the on_error callback.
I've just submitted a pull request that solves this issue. The solution I come up with is that after an exception occur, it calls the on_error callback before exiting the main loop. So one can handle the exceptions in the on_error callback.
Use Unicorn. I've moved to Unicorn and that's fixed a lot of my issues with this connector. Also reduce the number of ingestible requests, for instance from 100ms to 1000ms. Better to have 1s data with good integrity than 100ms data that crashes every minute (in my case).
I'm closing this issue as we published a brand new modularised version of the Python connector. Feel free to start using it and open a new issue if you encounter any problems.