Python Websockets:为什么客户端无法连接到我的服务器

时间:2021-07-09 19:08:36

标签: python websocket

我正在使用 websockets Python 库。

所以我有一个客户就是

# client.py
import websockets
import asyncio


async def main():
    print("SENDING")
    async with websockets.connect(f"ws://{IP}:8765") as connection:
        await connection.send("Poggers")
    print("END")


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

其中 IP 是我访问 https://whatismyipaddress.com 时获得的 IPv4 地址。

我的服务器代码是

# server.py
import asyncio
import websockets


async def handle_server(websocket, path):
    async for message in websocket:
        print(message)


async def main():
    async with websockets.serve(handle_server, "0.0.0.0", 8765):
        await asyncio.Future()

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

假设我正在运行服务器代码 (server.py),而我的朋友正在运行客户端代码 (client.py),按此顺序。出于某种原因,他在屏幕上看到的只有

SENDING

它挂了。

我在屏幕上看不到任何东西。

这是为什么?

0 个答案:

没有答案