我尝试运行以下代码作为一个简单的聊天服务器:
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。
1条答案
按热度按时间ar5n3qh51#
你有没有尝试过使用广播的完全导入?