前言
本文主要介绍了通过Python3 的 Websockets模块来建立最简单的服务器/客户端连接通信的方法。此方法适用于嵌入式开发者对Python3 Websockets模块快速上手,了解相关原理后,即可让两设备进行数据通信。
至于如何安装Websockets模块,在上一篇文章我有说到。点击--> 在Ubuntu中安装Python3 websockets模块
本文参考资料:http://websockets.readthedocs.io/en/stable/intro.html
运行环境
Ubuntu 18.04
Vmware 12 Pro
Python 3.6.5(不能低于 Python 3.4)
服务器 Server
代码:
#!/usr/bin/python3
import websockets
import asyncio
async def hello(websocket,path):
name = await websocket.recv()
print(f"A new client : {name}")
greeting = "Welcome " + name
await websocket.send(greeting)
print(f"send '{greeting}' to '{name}'")
start_server = websockets.serve(hello,'localhost',8765)
asyncio.get_event_loop().run_until_c

本文提供了一种使用Python3的Websockets模块在嵌入式环境中建立简单服务器和客户端通信的方法。在Ubuntu 18.04环境下,通过运行服务器和客户端的代码示例,实现两设备的数据交互,适合初学者快速入门。

2460

被折叠的 条评论
为什么被折叠?



