您的位置:首页 > 其它

bottle框架学习(二)静态文件请求、404错误与URL转向

2018-03-13 22:37 519 查看

返回静态文件内容

return static_file(filename,root=”,mimetype=”)

from bottle import route,run,static_file

@route('/static/<filename:path>')
def index(filename):
return static_file(filename,root="static/")

run(host='localhost',port=80,debug=True,reloader=True)


打开浏览器http://localhost/static/img.gif



强制下载文件

return static_file(filename,root=”“,download=True)

download=True表示用服务器上的文件名进行保存

download=”my.jpg”表示下载保存的文件名

from bottle import route,run,static_file

@route('/static/<filename:path>')
def index(filename):
return static_file(filename,root="static/")

@route('/d1/<filename:path>')
def d1(filename):
return static_file(filename, root="static/",download="demo.gif")

run(host='localhost',port=80,debug=True,reloader=True)


打开浏览器http://localhost/d1/img.gif



指定404页面

@error(404)装饰错业务函数

业务函数的参数列表中要包含一个接受错误值的参数

from bottle import route,run,static_file,error

@route('/static/<filename:path>')
def index(filename):
return static_file(filename,root="static/")

@route('/d1/<filename:path>')
def d1(filename):
return static_file(filename, root="static/",download="demo.gif")

@error(404)
def err(err):
return "亲,你要的页面丢失了"

run(host='localhost',port=80,debug=True,reloader=True)




URL转向

转向错误abort(404,’error_info’)

from bottle import route,run,static_file,error,abort

@route('/static/<filename:path>')
def index(filename):
return static_file(filename,root="static/")

@route('/d1/<filename:path>')
def d1(filename):
return static_file(filename, root="static/",download="demo.gif")

@error(404)
def err(err):
return "亲,你要的页面丢失了404"

@error(500)
def err(err):
return "亲,你要的页面丢失了500"

@route('/abort')
def abort_test():
abort(500,'error_info')

run(host='localhost',port=80,debug=True,reloader=True)




调用redirect(other url)函数

from bottle import route,run,static_file,error,abort,redirect

@route('/static/<filename:path>')
def index(filename):
return static_file(filename,root="static/")

@route('/d1/<filename:path>')
def d1(filename):
return static_file(filename, root="static/",download="demo.gif")

@error(404)
def err(err):
return "亲,你要的页面丢失了404"

@error(500)
def err(err):
return "亲,你要的页面丢失了500"

@route('/abort')
def abort_test():
abort(500,'error_info')

@route('/')
def index():
return "index Page"

@route('/login/<name>')
def login(name):
if name == "abc":
redirect('/')
else:
redirect('/login')

run(host='localhost',port=80,debug=True,reloader=True)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐