對于一個框架來說,最主要的功能就是提供路由功能。而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 來查看結果*