如何重新连接WebSocket连接websocket-client

hgqdbh6s  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(449)

我一直在尝试编写从Binance收集加密数据的代码。Binance在24小时后自动断开连接。在断开连接后有什么方法可以让我重新连接吗?我相信永远运行应该会为我解决这个问题。但是当一个错误被抛出时它就死了。我将在服务器上全天候运行这个程序。我还需要一种被通知的方式,也许是电报/我可以构建一个不和谐的机器人,当它断开连接时,我在哪里输入要发送的代码
这是我得到的错误。

Traceback (most recent call last):
  File "exchanges/binance/binance_ticker.py", line 97, in <module>
    start()
  File "exchanges/binance/binance_ticker.py", line 94, in start
    rel.dispatch()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/rel.py", line 205, in dispatch
    registrar.dispatch()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 72, in dispatch
    if not self.loop():
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 81, in loop
    e = self.check_events()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 232, in check_events
    self.callback('read', fd)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 125, in callback
    self.events[etype][fd].callback()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/listener.py", line 108, in callback
    if not self.cb(*self.args) and not self.persist and self.active:
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_app.py", line 349, in read
    op_code, frame = self.sock.recv_data_frame(True)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_core.py", line 401, in recv_data_frame
    frame = self.recv_frame()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_core.py", line 440, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_abnf.py", line 352, in recv_frame
    payload = self.recv_strict(length)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_abnf.py", line 373, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_core.py", line 524, in _recv
    return recv(self.sock, bufsize)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_socket.py", line 122, in recv
    raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

我的代码:

import websocket
import rel

uri = "wss://stream.binance.com:9443/ws/!ticker@arr"

def on_message(ws, message):
print(message)

def on_error(ws, error):
print(error)
write_logs(error)

def on_close(ws, close_status_code, close_msg):
print("### closed ###")
write_logs(str(close_status_code) + str(close_msg))
start(

def on_open(ws):
print("Opened connection")

start()
websocket.enableTrace(True)
ws = websocket.WebSocketApp(uri,
on_open = on_open,
on_message=on_message,
on_error = on_error,
on_close (on_close)
ws.run_forever(dispatcher=rel) #Set the dispatcher to automatic reconnection.
rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()

start()
kpbwa7wx

kpbwa7wx1#

这一行代码中的注解ws.run_forever(dispatcher=rel) #Set the dispatcher to automatic reconnection.是否可以根据rel模块自动重新连接?以及rel模块和func调度器如何协同工作?

相关问题