Python无法识别websockets.broadcast()

vnzz0bqm  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(134)

我尝试运行以下代码作为一个简单的聊天服务器:

import asyncio
import websockets

async def chat(websocket, debug):
    print(debug, type(debug))
    while True:
        message = await websocket.recv()
        await websockets.broadcast(message)
    
async def main():
    async with websockets.serve(chat, "localhost", 8008):
        await asyncio.sleep(1)
        await asyncio.Future()

if (__name__ == "__main__"):
    print(websockets.__file__)
    asyncio.run(main())

但是,当我这样做时,我得到以下错误:AttributeError: module 'websockets' has no attribute 'broadcast'
我试着检查导入模块的代码,确保没有名为websockets.py的文件。不知道为什么,因为这应该是最新版本的websockets。

ar5n3qh5

ar5n3qh51#

你有没有尝试过使用广播的完全导入?

from websockets.legacy.protocol import broadcast

相关问题