我正在关注YouTube上的websockets教程。服务器和客户端代码如下。我把笔记本电脑用作服务器,将树莓派用作客户端。
从命令提示符运行服务器文件时,一切正常。但是,当我从Pycharm运行服务器(单击“运行”按钮)时,Raspberry Pi的客户端无法连接到服务器。
server.py
import asyncio
import websockets
async def response(websocket, path):
message = await websocket.recv()
print(f"We got the message from the client: {message}")
await websocket.send("I can confirm I got your message!")
start_server = websockets.serve(response,'192.168.137.1', 1234)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
client.py
import asyncio
import websockets
async def message():
async with websockets.connect("ws://192.168.137.1:1234") as socket:
msg = input("What do you want to send: ")
await socket.send(msg)
print(await socket.recv())
asyncio.get_event_loop().run_until_complete(message())
有人可以告诉我如何从Pycharm运行服务器吗?比转到命令提示符方便得多。