首页 > 解决方案 > 如何在 python 中使用 websockets 流式传输信息?

问题描述

我正在自学 websockets 并编写了这段代码

import asyncio
import websockets

async def stream_msgs(uri,msg):
    async with websockets.connect(uri) as websocket:
        await websocket.send(msg)
        rec= await websocket.recv()
        print(f"< {rec}")



asyncio.get_event_loop().run_until_complete( stream_msgs('wss://ws.blockchain.info/inv',"{op:unconfirmed_sub}"))

问题是它在收到消息后退出程序。当我用这个工具进行测试时,同样是流信息。如何在 python 中使用 websockets 流式传输数据?

标签: pythonwebsocketasync-awaitpython-asyncio

解决方案


推荐阅读