Websocket 断开连接
我正在使用 python websocket-client 订阅 kraken 交换 websocket。系统最初运行一两天正常,但随后开始面临与 cloudflare 的断开连接,并出现错误
GET / HTTP/1.1
Upgrade: websocket
Host: ws.kraken.com
Origin: http://ws.kraken.com
***
and after many reattempts by the system websockets gives the error
*** error from callback <function on_close at 0x000001F33E2CBAC0>: maximum recursion depth exceeded while calling a Python object
我的实施
def on_close(ws, close_status_code, close_msg):
log.info("Connection Closed:" + str(close_status_code)+" " + str(close_msg))
print("Connection Closed:" + str(close_status_code)+" " + str(close_msg))
print("Retry : %s" % time.ctime())
time.sleep(10)
print("restarting bot")
connect_websocket() # retry per 10 seconds
def on_open(ws):
# print(symbol)
print('connection established')
string = {"event": "subscribe", "pair": ["ADA/USD", "BCH/USD", "DOT/USD", "ENJ/USD", "ETC/USD", "ETH/USD", "FIL/USD", "GNO/USD", "KSM/USD", "LTC/USD", "OMG/USD", "SC/USD", "SOL/USD", "DOGE/USD", "XRP/USD", "SHIB/USD", "AXS/USD", "BAL/USD", "COMP/USD", "CRV/USD", "CQT/USD", "DASH/USD", "EOS/USD", "EWT/USD", "GHST/USD", "GRT/USD", "KAR/USD", "LINK/USD", "LPT/USD", "LSK/USD", "MANA/USD", "MINA/USD", "MOVR/USD", "OXT/USD", "QTUM/USD", "SAND/USD", "TRX/USD", "XLM/USD", "XMR/USD", "ZEC/USD", "BTC/USD"], "subscription": {"interval": 240, "name": "ohlc"}}
ws.send(json.dumps(string))
def on_message(ws, message):
print(message)
message = json.loads(message, parse_float=decimal)
try:
if message["event"] == 'heartbeat':
return
except:
log.info(message)
def connect_websocket(): ## TO CONNECT TO WEBSOCKET, CALL ON EACH bot restart command
websocket.enableTrace(True, log)
global ws
ws = websocket.WebSocketApp("wss://ws.kraken.com/", on_open=on_open,
on_close=on_close,
on_message = on_message)
print("symbol timeframe ", str(240))
ws.run_forever()
I am using python websocket-client to subscribe to kraken exchange websocket. The systems works fine initially for a day or two but then it start facing disconnection from cloudflare with error
GET / HTTP/1.1
Upgrade: websocket
Host: ws.kraken.com
Origin: http://ws.kraken.com
***
and after many reattempts by the system websockets gives the error
*** error from callback <function on_close at 0x000001F33E2CBAC0>: maximum recursion depth exceeded while calling a Python object
My implementation
def on_close(ws, close_status_code, close_msg):
log.info("Connection Closed:" + str(close_status_code)+" " + str(close_msg))
print("Connection Closed:" + str(close_status_code)+" " + str(close_msg))
print("Retry : %s" % time.ctime())
time.sleep(10)
print("restarting bot")
connect_websocket() # retry per 10 seconds
def on_open(ws):
# print(symbol)
print('connection established')
string = {"event": "subscribe", "pair": ["ADA/USD", "BCH/USD", "DOT/USD", "ENJ/USD", "ETC/USD", "ETH/USD", "FIL/USD", "GNO/USD", "KSM/USD", "LTC/USD", "OMG/USD", "SC/USD", "SOL/USD", "DOGE/USD", "XRP/USD", "SHIB/USD", "AXS/USD", "BAL/USD", "COMP/USD", "CRV/USD", "CQT/USD", "DASH/USD", "EOS/USD", "EWT/USD", "GHST/USD", "GRT/USD", "KAR/USD", "LINK/USD", "LPT/USD", "LSK/USD", "MANA/USD", "MINA/USD", "MOVR/USD", "OXT/USD", "QTUM/USD", "SAND/USD", "TRX/USD", "XLM/USD", "XMR/USD", "ZEC/USD", "BTC/USD"], "subscription": {"interval": 240, "name": "ohlc"}}
ws.send(json.dumps(string))
def on_message(ws, message):
print(message)
message = json.loads(message, parse_float=decimal)
try:
if message["event"] == 'heartbeat':
return
except:
log.info(message)
def connect_websocket(): ## TO CONNECT TO WEBSOCKET, CALL ON EACH bot restart command
websocket.enableTrace(True, log)
global ws
ws = websocket.WebSocketApp("wss://ws.kraken.com/", on_open=on_open,
on_close=on_close,
on_message = on_message)
print("symbol timeframe ", str(240))
ws.run_forever()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在脚本的开头尝试一下,为我工作
Try this in the beginning of the script, worked for me