我遇到了与此python websockets的github问题相同的问题:https : //github.com/aaugustin/websockets/issues/367
提议的解决方案对我不起作用。我得到的错误是:
websockets.exceptions.ConnectionClosed:WebSocket连接已关闭:代码= 1006(连接异常关闭[内部]),没有原因
这是我的代码:
async def get_order_book(symbol):
with open('test.csv', 'a+') as csvfile:
csvw = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
DT = Data(data=data, last_OB_id=ob_id, last_TR_id=tr_id, sz=10, csvw=csvw)
while True:
if not websocket.open:
print('Reconnecting')
websocket = await websockets.connect(ws_url)
else:
resp = await websocket.recv()
update = ujson.loads(resp)
DT.update_data(update)
async def get_order_books():
r = requests.get(url='https://api.binance.com/api/v1/ticker/24hr')
await asyncio.gather(*[get_order_book(data['symbol']) for data in r.json()])
if __name__ == '__main__':
asyncio.run(get_order_books())
Run Code Online (Sandbox Code Playgroud)
我一直在测试它的方法是关闭我的互联网连接,但是经过十秒钟的延迟后,它仍然返回1006错误。
我正在运行Python 3.7和Websockets 7.0。
让我知道您的想法,谢谢!