binance-connector-python icon indicating copy to clipboard operation
binance-connector-python copied to clipboard

Connection to remote host was lost

Open sunshineinwater opened this issue 1 year ago • 6 comments

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

sunshineinwater avatar Sep 18 '23 22:09 sunshineinwater