Bottle实例2——Request Routing
来源:互联网 发布:linux修改启动画面 编辑:程序博客网 时间:2024/06/09 18:35
1、代码实例1:
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
# FileName: HelloWorld.py
#导入bottle模块
import bottle
#使用bottle模块中的route, run, template函数
@bottle.route('/')
@bottle.route('/hello/<name>')
def greet(name='Stranger'):
return bottle.template('Hello {{name}}, how are you?', name=name)
bottle.run(host='localhost', port=8080)
这里使用了bottle的template模板,运行程序之后。
在浏览器中输入:http://localhost:8080/ 会得到如下:
在浏览器中输入:http://localhost:8080/hello/Johnny 得到的如下:
这里'/hello/<name>'中的name是一个通配符,跟随输入的不同,显示不同。
route()修饰器用来绑定一个URL到一个回调函数,为默认程序添加一条新的路由;你可以为一个回调函数绑定多条路由(如上例所示)。你可以为你的URL添加通配符(如上例的'name)', 通过关键字参数的值来访问它们。
2、Dynamic Routes(动态路由)
包含通配符(wildcards)的路由叫做动态路由;可以在同一时间匹配多个URL,一个简单的通配符一般放在尖括号里,如:<name>。
每个通配符覆盖的URL部分作为关键字参数传递个回调函数,如上例name传递给greet回调函数。
示例:
2
3
4
5
6
def show_wiki_page(pagename):
...
@route(’/<action>/<user>’) # matches /follow/defnull
def user_api(action, user):
...
<name:filter> or <name:filter:config> 语法中config部分取决于filter使用的值;
目前filter匹配的值如下(后续可能还会增加):
• :int matches (signed) digits only and converts the value to integer.
• :float similar to :int but for decimal numbers.
• :path matches all characters including the slash character in a non-greedy way and can be used to match more than one path segment.
• :reallows you to specify a custom regular expression(正则表达式) in the config field. The matched value is not modified.
实际的例子如下:
2
3
4
5
6
7
8
9
10
11
def callback(id):
assert isinstance(id, int)
@route(’/show/<name:re:[a-z]+>’)
def callback(name):
assert name.isalpha()
@route(’/static/<path:path>’)
def callback(path):
return static_file(path, ...)
3.HTTP Request Methods( HTTP 请求方法)
HTTP的请求方法有:GET、POST、PUT、DELETE;Bottle中,在没有明确指定请求方法式, GET是所有路由中默认的方法,要使用其他请求方法,请在route()修饰器中添加methond关键字参数,或者使用:get()、post()、put()、deltet()四个修饰器函数之一;
实例1(使用post()等函数):
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
# FileName: http_request_methods.py
# Time: 2014-8-24 12:39:32
import bottle
def check_login(username, password):
if username == '123' and password == '234':
return True
else:
return False
@bottle.get('/login')
def login():
return ''' <form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit">
</form>
'''
@bottle.post('/login')
def do_login():
username = bottle.request.forms.get('username')
password = bottle.request.forms.get('password')
if check_login(username, password):
return "<p> Your login information was correct.</p>"
else:
return "<p>Login failed.</p>"
bottle.run(host='localhost', port=8080)
输入Username:123 password:234 显示如下:
实例2(route()方式):
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def check_login(username, password):
if username == '123' and password == '234':
return True
else:
return False
@bottle.route('/login')
def login():
return ''' <form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit">
</form>
'''
@bottle.route('/login', method='POST')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
if check_login(username, password):
return "<p> Your login information was correct.</p>"
else:
return "<p>Login failed. </p>"
bottle.run(host='localhost', port=8080)
- Bottle实例2——Request Routing
- Bottle实例1——HelloWord
- Bottle实例之—POST、GET再解
- Bottle实例Todo-List—用SQLite3创建数据库
- Bottle实例Todo-List—编辑数据库中的记录
- Python框架——bottle
- Bottle实例Todo-List—查询并显示数据库中的内容
- Bottle实例Todo-List—在数据库中插入一条记录
- Bottle实例Todo-List—动态路由中使用正则表达式
- Bottle实例Todo-List—返回静态文件和json对象
- bottle 接口实例
- ARR:Application Request Routing
- Bottle实例—重定向函数redirect返回静态文本和相对路径与绝对路径( Absolute and Relative Paths)问题
- Bottle实例之-MySQL连接
- IIS7安装Application Request Routing
- Bottle.py的教程翻译(一)——安装
- Python bottle 笔记(一) —— Hello World
- Python bottle 笔记(二) —— APP
- LeetCode: Set Matrix Zeroes
- Android Bitmap详细介绍
- hdu4971 流-最大权闭包
- fzoj 1559 Count Zeros(数学:推理)
- JavaScript特效制作经典精讲(案例入门详解、可直接粘贴拷贝运行、史上最牛案例)
- Bottle实例2——Request Routing
- PHP访问WebService
- 用SSH实现简单的删除操作时,id传入后台报空的问题。
- android Bitmap学习总结
- 纯虚函数可以定义了
- Ubuntu 下安装 JDK
- ORACLE 创建表空间,用户,权限
- hdu 1864 最大报销额 模型为简单的01背包
- 炼数成金---Rcur网络爬虫