python轻量级web框架_python bottle轻量级web框架

Bottle是一个轻量级的Web框架,此框架只由一个 bottle.py 文件构成,不依赖任何第三方模块。

#!/usr/bin/env python#-*- coding:utf-8 -*-

from bottle importtemplate, Bottle

app=Bottle()

@app.route('/say')defindex():return "Hello World"

#return template('Hello {{name}}!', name="bottle")

if __name__ == '__main__':

app.run(server="tornado",host='0.0.0.0', port=8888)

1、路由系统

路由系统是的url对应指定函数,当用户请求某个url时,就由指定函数处理当前请求,对于Bottle的路由系统可以分为一下几类:

静态路由

动态路由

请求方法路由

二级路由

1.1静态路由

@app.route("/login") #默认为get请求

defhello():return """

Username:

Password:

"""@app.route("/login",method="POST")defdo_login():

username= request.forms.get("username")

password= request.forms.get("password")print(username,password)if username andpassword:return "

login success

"

else:return "

login failure

"

1.2动态路由

@app.route('/say/')defcallback(name):return template('Hello {{name}}!')

@app.route('/say/')defcallback(id):return template('Hello {{id}}!')

@app.route('/say/')defcallback(name):return template('Hello {{name}}!')

@app.route('/static/')defcallback(path):return static_file(path, root='static')

1.3请求方法路由

@app.route('/hello/', method='POST') #等同于@app.post('/hello/')

defindex():

...

@app.get('/hello/') #等同于@app.route('/hello/',method='GET')

defindex():

...

@app.post('/hello/') #等同于@app.route('/hello/',method='POST')

defindex():

...

@app.put('/hello/') #等同于@app.route('/hello/',method='PUT')

defindex():

...

@app.delete('/hello/')defindex():

...

1.4二级路由

1 #!/usr/bin/env python

2 #-*- coding:utf-8 -*-

3 from bottle importtemplate, Bottle4

5 app01 =Bottle()6

7 @app01.route('/hello/', method='GET')8 defindex():9 return template('App01!')10

11

12

13 app01.py

#!/usr/bin/env python#-*- coding:utf-8 -*-

from bottle importtemplate, Bottle

app02=Bottle()

@app02.route('/hello/', method='GET')defindex():return template('App02!')

app02.py

#!/usr/bin/env python#-*- coding:utf-8 -*-

from bottle importtemplate, Bottlefrom bottle importstatic_file

app=Bottle()

@app.route('/hello/')defindex():return template('Root {{name}}!', name="bottle")from root_dir importapp01from root_dir importapp02

app.mount('app01', app01.app01)

app.mount('app02', app02.app02)

app.run(host='localhost', port=8888)

1.5静态文件映射,static_file()函数用于响应静态文件的请求

#静态文件映射,static_file()函数用于响应静态文件 的请求

@app.route("/static/")defsend_image(filename):return static_file(filename, root=os.getcwd(), mimetype="image/jpg")

@app.route("/static/") #可匹配路径

defsend_image(filename):return static_file(filename, root=os.getcwd(), mimetype="image/jpg")#强制下载

@app.route("/static/") #可匹配路径

defdownload(filename):return static_file(filename, root=os.getcwd(), download=filename)

1.6使用error()函数自定义错误页面

@app.error(404)deferror404(error):return "我找不到目标了,我发生错误了"

1.7HTTP错误和重定向

abort()函数是生成HTTP错误的页面的一个捷径

@app.route("/restricted")defrestricted()

abort(401,"Sorry, access denied")#将url重定向到其他url,可以在location中设置新的url,接着返回一个303

# redirect()函数可以帮助我们做这件事

@app.route("/wrong/url")defwrong()

redirect("/right/url")

其他异常

除了HTTPResponse或者HTTPError以外的其他异常,都会导致500错误,因此不会造成WSGI服务器崩溃

将bottle.app().catchall的值设为False来关闭这种行为,以便在中间件中处理异常

2.cookies

@app.route("/login", method="POST")defdo_login():

username= request.forms.get("username")

password= request.forms.get("password")print(username, password)if username andpassword:

response.set_cookie("name",username, secret= 'some-secret-key') #设置cookie

return "

login success

"

else:return "

login failure

"@app.route("/static/")defsend_image(filename):

username= request.get_cookie("name", secret= 'some-secret-key') #获取cookie

ifusername:return static_file(filename, root=os.getcwd(), mimetype="image/jpg")else:return "verify failed"

bottle就的 set_cookie 的默认 path 是当前路径,也就是说,在这个页面上存入的 cookie 在别的页面通常是取不到的,不熟悉这点的人几乎都要栽在这里。而且更坑的是:set_cookie 有 path 参数可以指定 path ,但 get_cookie 却没有这个 path 参数可选——也就是说,你即使设置了其它 path ,如果 get_cookie 的时候不是刚好在那个 path 下的话,也取不到……

解决方法:把所有的 cookie 都放到"/"下面,至少目前用下来感觉没问题。

注:request.query 或 request.forms 都是一个 FormDict 类型,

其特点是:当以属性方式访问数据时——如 request.query.name,返回的结果是 unicode ,当以字典试访问数据时,如 :request.query['name']或者request.query.get("name"),则返回的结果是原编码字符串

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值