LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

Python web.py 一个轻量级、简洁的 Python Web 框架简单入门

admin
2025年7月15日 10:30 本文热度 111

web.py 是一个轻量级、简洁的 Python Web 框架,由 Aaron Swartz 开发。它以简单直接的设计哲学著称,适合快速构建小型 Web 应用和 API接口实现。

核心特点:

  1. 1. 极简设计:代码量少,核心文件仅一个 web 模块
  2. 2. 零配置:开箱即用,无需复杂配置
  3. 3. URL 路由:灵活的 URL 映射机制
  4. 4. 模板引擎:内置简单模板系统
  5. 5. 数据库支持:内置数据库访问工具
  6. 6. HTTP 工具:完善的请求/响应处理

安装 web.py

通过 pip 安装:

pip install web.py

注意:Python 3 请使用 pip install web.py==0.62(0.62+ 版本支持 Python 3)


基础配置

创建基础应用结构:

myapp/
├── app.py         # 主程序
├── templates/     # 模板目录
   └── index.html
└── static/        # 静态文件
    └── style.css

使用示例

示例 1:基础 Web 应用

# app.py
import web

# URL 路由配置
urls =(
    '/','Index',
    '/hello/(.*)','Hello'
)

app = web.application(urls,globals())

classIndex:
    defGET(self):
        return"Welcome to web.py!"

classHello:
    defGET(self, name):
        returnf"Hello, {name}!"

if __name__ =="__main__":
    app.run()

运行应用:

python app.py

访问:

  • • http://localhost:8080/
  • • http://localhost:8080/hello/Leo

示例 2:使用模板引擎

<!-- templates/index.html -->
<html>
<head><title>web.py Demo</title></head>
<body>
    <h1>$title</h1>
    <ul>
    $for item in items:
        <li>$item</li>
    </ul>
</body>
</html>
# app.py (添加新路由)
urls =(
    '/','Index',
    '/list','List'
)

render = web.template.render('templates/')

classList:
    defGET(self):
        data ={
            "title":"Shopping List",
            "items":["Apples","Bananas","Coffee"]
        }
        return render.index(**data)

示例 3:表单处理

# 添加表单路由
urls =(
'/form','FormHandler'
)

classFormHandler:
defGET(self):
return'''<form method="post">
                    <input type="text" name="name">
                    <button>Submit</button>
                  </form>'''


defPOST(self):
        data = web.input()
return f"Received: {data.name}"

示例 4:数据库操作(SQLite)

# 添加表单路由
urls =(
    '/form','FormHandler'
)

classFormHandler:
    defGET(self):
        return'''<form method="post">
                    <input type="text" name="name">
                    <button>Submit</button>
                  </form>'''

    
    defPOST(self):
        data = web.input()
        return f"Received: {data.name}"

关键组件详解

  1. 1. URL 路由系统
    urls = (
        '/path', 'ClassName',
        '/(.*)', 'CatchAll'  # 正则匹配
    )
  2. 2. 请求处理
    class MyHandler:
        def GET(self):
            # 获取查询参数
            input_data = web.input(id=None)
            
        def POST(self):
            # 获取表单数据
            form_data = web.input()
  3. 3. 模板使用
    render = web.template.render('templates/')
    return render.my_template(name="John", age=30)
  4. 4. HTTP 工具
    # 重定向
    raise web.seeother('/new-url')

    # 设置 Cookie
    web.setcookie("user", "john")

部署生产环境

使用内置 WSGI 服务器部署:

# app.py 底部添加
application = app.wsgifunc()

然后用 uWSGI/Gunicorn 运行:

gunicorn app:application

适用场景

  • • 快速原型开发
  • • 小型 REST API
  • • 教学/学习 Web 开发
  • • 微服务端点
  • • 需要轻量级框架的项目

注意:对于大型复杂项目,建议使用 Django 或 Flask 等更全功能的框架。

最近在看官方微信公众号开发指南和chatgpt-on-wechat都是基于web.py进行简单示例讲解和开发的,所以很有必要学习和记录一下web.py模块。 web.py 以其极简哲学著称,核心代码仅数千行,但提供了完整的 Web 开发基础功能,是理解 Web 框架设计的优秀学习资源。


该文章在 2025/7/15 10:30:28 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved