多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
对于一个框架来说,最主要的功能就是提供路由功能。而bottle框是通过修饰器来提供路由功能的。而绑定路由可以使用Bottle类的三个方法:route,get,post,下面讲解一下这三个方法。 * route第一个参数为路由规则,然后可提供一个method参数(可以是列表)来决定响应get和post请求。 ~~~ # coding:UTF-8 from bottle import Bottle app = Bottle() @app.route("/", method=['GET', 'POST']) def index(): return "hello world !" app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ * get函数只需要提供路由规则,但只能处理get请求,如果post请求 符合路由规则会报405错误 ~~~ # coding:UTF-8 from bottle import Bottle app = Bottle() @app.get("/") def index(): return "hello world !" app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ * post函数只需要提供路由规则,但只能处理post请求,如果get请求 符合路由规则会报405错误 ~~~ # coding:UTF-8 from bottle import Bottle app = Bottle() @app.post("/") def index(): return "hello world !" app.run(host="127.0.0.1", port=8000, reloader=True, debug=True) ~~~ *以上三个代码都是可以访问 http://127.0.0.1:8000 来查看结果*