ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
对于一个框架来说,最主要的功能就是提供路由功能。而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 来查看结果*