You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Web服务器控制LED开关并反馈状态技术求助

Python Web服务器实现LED控制与状态展示

需求回顾

你想要实现的是:通过网页按钮控制LED开关,同时在网页上实时显示LED当前的状态。你的现有代码已经用到了web.py框架,不过网页部分还没补全,我来帮你完善并优化整个功能~

第一步:准备工作

首先确保安装了web.py框架,终端执行以下命令:

pip install web.py

如果是用树莓派这类硬件控制真实LED,还需要安装GPIO库:

pip install RPi.GPIO

完整代码实现

主服务器代码(server.py)

import web
# 导入GPIO库(控制真实LED时需要,模拟测试可注释)
import RPi.GPIO as GPIO

# 初始化LED状态:0=关闭,1=开启
global status
status = [0]

# GPIO硬件配置(树莓派专用,根据你的引脚调整)
LED_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)  # 初始状态为关闭

# 渲染templates目录下的网页模板
render = web.template.render('templates/')

# URL路由规则:根路径映射到hello处理类
urls = (
    '/', 'hello'
)
app = web.application(urls, globals(), True)

class hello:
    def GET(self):
        # 获取URL中的turn控制参数
        getInput = web.input(turn="")
        command = str(getInput.turn).lower()
        
        # 根据指令更新LED状态
        if command == "on":
            status[0] = 1
            # 控制硬件LED开启(模拟测试可注释)
            GPIO.output(LED_PIN, GPIO.HIGH)
        elif command == "off":
            status[0] = 0
            # 控制硬件LED关闭(模拟测试可注释)
            GPIO.output(LED_PIN, GPIO.LOW)
        
        # 渲染网页模板,传递当前LED状态
        return render.index(status=status[0])

# 程序退出时清理GPIO资源(树莓派专用)
def cleanup():
    GPIO.cleanup()

if __name__ == "__main__":
    try:
        app.run()
    finally:
        cleanup()

网页模板文件(templates/index.html)

在项目根目录下创建templates文件夹,里面新建index.html文件:

<!DOCTYPE html>
<html>
<head>
    <title>LED控制器</title>
    <style>
        .container {
            text-align: center;
            margin-top: 60px;
        }
        .status-display {
            font-size: 26px;
            margin-bottom: 30px;
        }
        .control-btn {
            padding: 12px 24px;
            font-size: 20px;
            margin: 0 15px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            color: white;
        }
        .on-btn {
            background-color: #2ecc71;
        }
        .off-btn {
            background-color: #e74c3c;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="status-display">
            LED当前状态:{% if status == 1 %}<span style="color:#2ecc71;">已开启</span>{% else %}<span style="color:#e74c3c;">已关闭</span>{% endif %}
        </div>
        <a href="/?turn=on"><button class="control-btn on-btn">开启LED</button></a>
        <a href="/?turn=off"><button class="control-btn off-btn">关闭LED</button></a>
    </div>
</body>
</html>

代码功能说明

  • 状态管理:用全局列表status存储LED状态(用列表是因为Python中全局变量在函数内修改需要可变类型)
  • 请求处理:GET方法解析URL中的turn参数,根据指令更新状态,同时控制硬件LED(模拟测试可跳过硬件控制部分)
  • 动态网页:通过web.py的模板引擎将实时状态传递给HTML,动态展示状态文本颜色和按钮交互
  • 硬件兼容:如果只是做网页模拟,直接注释掉所有GPIO相关代码即可,状态仍会在网页上正常展示

测试方法

  1. 把主代码保存为server.py,创建templates文件夹并放入index.html
  2. 运行脚本:python server.py
  3. 打开浏览器访问http://localhost:8080(树莓派用户用设备IP地址代替localhost)
  4. 点击按钮就能看到LED状态实时更新啦~

内容的提问来源于stack exchange,提问作者Pewis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:40:44