Bottle:Python Web 框架¶
Bottle 是一个快速、简单且轻量级的用于 Python 的 WSGI 微型 Web 框架。它以单个文件模块的形式发布,除 Python 标准库外没有任何依赖。
路由: 请求到函数调用的映射,支持清晰的动态 URL。
工具: 方便地访问表单数据、文件上传、cookie、请求头及其他 HTTP 特性。
服务器: 内置 HTTP 开发服务器,并支持多种 WSGI 兼容的 HTTP 服务器(例如 gunicorn、paste 或 cheroot)。
示例:Bottle 中的“Hello World”
from bottle import route, run, template
@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
运行此脚本或将其粘贴到 Python 控制台,然后将浏览器指向 http://localhost:8080/hello/world。就这么简单。
下载和安装¶
使用 pip install bottle
安装最新的稳定版本,或将 bottle.py(不稳定版)下载到你的项目目录中。除 Python 标准库外,没有硬性[1]依赖。
停止维护的 Python 版本¶
Bottle 在 0.12 及之前的版本支持过多的 Python 版本,其中一些早在十多年前就已停止维护。从 Bottle 0.13 开始,我们仅确保向后兼容 仍在维护的 Python 版本。过时的 Python 版本可能仍能工作,但不再测试其兼容性。
如果您不幸必须在生产环境依赖“已停止维护的 Python 版本”,请继续使用 Bottle 0.12 (LTS) 或 Bottle 其他仍支持它的版本。其他人应定期升级,以获得新功能和改进。
Bottle 版本 |
Python 2 |
Python 3 |
---|---|---|
0.12 |
2.5 - 2.7 |
3.2 - 3.12 |
0.13 |
2.7 |
>=3.8 [2] |
0.14(计划中) |
不再支持 |
>=3.9 [2] |
文档¶
入门
高级主题
许可证¶
代码和文档均根据 MIT 许可证提供
Copyright (c) 2009-2025, Marcel Hellkamp.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
然而,Bottle 徽标 不受 该许可证约束。允许将此徽标用作 Bottle 主页的链接或直接与未修改的库相关联。在所有其他情况下,请先征得同意。
脚注