python bottle框架 重定向_Bottle:Python Web 框架

转载自:http://article.yeeyan.org/view/35282/136724

0.1. 核心特征

0.2. 下载/安装

1. 特征和示例

1.1. 路由

1.2. 模板

1.3. 静态文件、重定向和 HTTP 错误

1.4. POST、GET、头信息和 Cookies

1.5. HTTP 服务器

1.6. 没有的功能和已经的 Bugs

2. 许可证(MIT)

Bottle 是一个快速而简单的、由 Python 包装为一个单一文件的、没有额外依赖的 WSGI Web 框架。

0.1. 核心特征

路由:使用一个简单但有力的模式语法,映射 URLs 到代码。

模板:快速的内建模板引擎,并且支持 mako、jinja2 和 cheetah 模板。

服务器:内建 HTTP 开发服务器,并支持 paste、fapws3、flup、cherrypy 或其它任何有 WSGI 功能的服务器。

无依赖:所有东西都在一个单一的文件中,并且不依赖其它任何 Python 标准库。

0.2. 下载/安装

可以使用 easy_install -U bottle 来安装最后的稳定发行版,或仅下载最新的 bottle.py

并将它放置在你的项目目录里。没有任何对 Python 标准库的依赖。Bottle 运行在 Python 2.5+ 或 3.x(使用

2to3)上。

1. 特征和示例

不需要安装或配置。不依赖 Python 标准库。只需要取得 bottle.py,将它放置在你的项目目录并开始编码即可。

from bottle import route, run

@route('/')

def index():

return 'Hello World!'

run(host = 'localhost', port = 8080)

这就是所有。运行你的代码并访问 http://localhost:8080/。

1.1. 路由

使用 @route() 修饰器来绑定 URLs 到你的处理函数。命名参数可被用来生成更美观的 URLs。

@route('/hello/:name')

def hello(name):

return 'Hello, %s' % name

1.2. 模板

Bottle 包含一个名为 SimpleTemplate 的简单和轻量快速的模板引擎。仅返回一个填充了模板变量的字典和传递一个模板名称给 @view 修饰器。

@route('/hello/template/:names')

@view('hello')

def template_hello(names):

names = names.split(',')

return dict(title = 'Hello World', names = names)

接着这里是模板“./views/hello.tpl”:

〈html〉

〈head〉

〈title〉{{title}}〈/title〉

〈/head〉

〈body〉

%for name in names:

〈p〉你好,〈strong〉{{name}}〈/strong〉〈/p〉

%end

〈/body〉

〈/html〉

Bottle 让切换其它模板引擎变得简单。支持 mako、jinja2 和 cheetah。

from bottle import mako_view as view

1.3. 静态文件、重定向和 HTTP 错误

使用这些便利的帮助来进行常规的任务。

from bottle import send_file, redirect, abort

@route('/static/:filename')

def static_file(filename):

send_file(filename, root = '/path/to/static/files')

@route('/wrong/url')

def wrong():

redirect('/right/url')

@route('/restricted')

def restricted():

abort(401, 'Sorry, access denied.')

1.4. POST、GET、头信息和 Cookies

就像使用一个 dict() 一样简单。

from bootle import request, response, route

@route('/hello/cookie')

def cookie():

name = request.COOKIES.get('name', 'Stranger')

response.headers['Content-Type'] = 'text/plain'

return 'Hello, %s' % name

@route('/hello/cookie', method = 'POST')

def set_cookie():

if 'name' in request.POST:

name = request.POST['name']

response.COOKIES['name'] = name

return 'OK'

1.5. HTTP 服务器

Bootle 有一个内建的 HTTP 服务器,但替代的也支持 cherrypy、flup、paste 和 fapws3。

from bottle import PasteServer

run(server = PasteServer)

1.6. 没有的功能和已知的 Bugs

Bottle 还未包括:

模型和 ORMs:选择你自己的(SQLAlchemy、Elixir)。

HTML-帮助、Session、身份和认证:由你自己完成。

脚手架:对不起,没有。

2. 许可证(MIT)

Copyright (c) 2009, 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值