Skip to main content

micropython-websocket_server

适用于ESP32的MicroPython WebSocket-server模块。

安装方法

在Thonny中安装到ESP32:

  1. 用USB将ESP32连接到电脑
  2. 打开Thonny,右下角切换为 MicroPython(ESP32)
  3. 菜单 Tools → Manage packages
  4. 搜索 micropython-websocket_server
  5. 点击安装

使用示例

import network
import micropython_websocket_server as websocket_server

def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print("连接 Wi-Fi...")
        wlan.connect("你的WiFi名称", "你的WiFi密码")
        while not wlan.isconnected():
            pass
    ip = wlan.ifconfig()[0]
    print("Wi-Fi 已连接,ESP32 IP 地址:", ip)
    print("请在 PC 端连接: ws://{}:8765".format(ip))
    return ip

do_connect()

# 创建并启动服务器
server = websocket_server.WebSocketServer("0.0.0.0", 8765)
server.start()
print("WebSocket 服务器已启动,等待连接...")

try:
    while True:
        # 阻塞等待一个客户端连接
        conn, addr = server.accept()
        if conn is None:
            continue   # 握手失败,继续等待

        print("客户端已连接:", addr)

        # 与该客户端通信直到断开
        try:
            while True:
                msg = conn.recv()         # 阻塞接收
                print("收到:", msg)
                conn.send(msg)            # 原样返回
        except websocket_server.WebSocketConnectionClosedException:
            print("客户端已断开:", addr)
        finally:
            conn.close()
            server.remove_client(conn)

finally:
    server.stop()

Metadata

Release files for micropython-websocket-server 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for micropython-websocket-server 0.1.0
File Size Uploaded Provenance Actions
micropython_websocket_server-0.1.0.tar.gz 12.0 kB Without provenance Details for micropython_websocket_server-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for micropython-websocket-server 0.1.0
File Interpreter ABI Platform Actions
micropython_websocket_server-0.1.0-py3-none-any.whl Python 3 none any Details for micropython_websocket_server-0.1.0-py3-none-any.whl

Total release size: 23.4 kB

Release files / micropython_websocket_server-0.1.0.tar.gz

Download URL micropython_websocket_server-0.1.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
103afa189efa4e99fa230880506615e2978858012e2634e738e53cb1e990aae2
BLAKE2b-256 checksum
How to use checksums
72e7caced98638d4a3841f3f253fc40d22c14d8e009caf4b84bb4439534125ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release files / micropython_websocket_server-0.1.0-py3-none-any.whl

Download URL micropython_websocket_server-0.1.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd6982c9a21135e177ce10ecb8d26f319ca41428333b38243c01778ed7e93ffc
BLAKE2b-256 checksum
How to use checksums
0709164090125f7e70a8af617232b069bbef6f797620599d4515765b7d074b87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Security for micropython-websocket-server 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page