Bottle实例2——Request Routing

来源:互联网 发布:linux修改启动画面 编辑:程序博客网 时间:2024/06/09 18:35

1、代码实例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- coding:  utf-8 -*-
#!/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回调函数。

示例:

1
2
3
4
5
6
@route(’/wiki/<pagename>’) # matches /wiki/Learning_Python
def show_wiki_page(pagename):
...
@route(’/<action>/<user>’) # matches /follow/defnull
def user_api(action, user):
...
(1)过滤器:过滤器(Filters)被用作定义多个通配符; 和在传递给回调函数之前转换其覆盖的URL部分,一个带过滤器的通配符被声明为如下方式:

<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.

实际的例子如下:

1
2
3
4
5
6
7
8
9
10
11
@route(’/object/<id:int>’)
def callback(id):
assert isinstance(idint)

@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()等函数):

1
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
# -*- coding:  utf-8 -*-
#!/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
   
在浏览器中输入:http://localhost:8080/login 显示如下;


输入Username:123   password:234  显示如下:


实例2(route()方式):

1
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
import bottle

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)
效果是一样的。


0 0
原创粉丝点击