【发布时间】:2019-06-03 17:50:15
【问题描述】:
我遇到了与 python websockets 的这个 github 问题相同的问题: https://github.com/aaugustin/websockets/issues/367
但建议的解决方案对我不起作用。我得到的错误是:
websockets.exceptions.ConnectionClosed:WebSocket连接关闭:code = 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())
我一直在测试它的方式是关闭我的互联网连接,但延迟十秒后它仍然返回 1006 错误。
我正在运行 Python 3.7 和 Websockets 7.0。
请告诉我你的想法,谢谢!
【问题讨论】:
标签: python python-3.x websocket python-asyncio