添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
import ssl

async def test_ws_quote():
bnwsuri = ‘wss://fstream.binance.com/stream?streams=btcusdt@bookTicker/ethusdt@bookTicker’
async with websockets.connect(bnwsuri,ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as ws:
while True:
recvData = await asyncio.wait_for(ws.recv(), timeout=30)
print(recvData)

if name == “ main ”:
asyncio.get_event_loop().run_until_complete(test_ws_quote())

but run it i can not receive any data, where is the error?

I directly copied your code, with only changing commas, indent and the __name__ == "__main__" part and can receive updates:

import asyncio
import websockets
import ssl
async def test_ws_quote():
        bnwsuri = "wss://fstream.binance.com/stream?streams=btcusdt@bookTicker/ethusdt@bookTicker"
        async with websockets.connect(bnwsuri,ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as ws:
                while True:
                        recvData = await asyncio.wait_for(ws.recv(), timeout=30)
                        print(recvData)
if __name__ == "__main__":
        asyncio.get_event_loop().run_until_complete(test_ws_quote())

Note: As good practice, you should put try and except blocks.