欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python輕量級web框架bottle使用方法解析

 更新時間:2020年06月13日 14:52:13   作者:_夕顏  
這篇文章主要介紹了Python輕量級web框架bottle使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

Bottle是一個輕量級的Web框架,此框架只由一個 bottle.py 文件構(gòu)成,不依賴任何第三方模塊。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle

app = Bottle()


@app.route('/say')
def index():
  return "Hello World"
  # return template('<b>Hello {{name}}</b>!', name="bottle")

if __name__ == '__main__':

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

1、路由系統(tǒng)

路由系統(tǒng)是的url對應(yīng)指定函數(shù),當(dāng)用戶請求某個url時,就由指定函數(shù)處理當(dāng)前請求,對于Bottle的路由系統(tǒng)可以分為一下幾類:

  • 靜態(tài)路由
  • 動態(tài)路由
  • 請求方法路由
  • 二級路由

1.1靜態(tài)路由

@app.route("/login") # 默認(rèn)為get請求
def hello():
  return """
  <form action="/login" method="post">
  Username:<input name="username" type="text" />
  Password:<input name="password" type="password" />
  <input value="Login" type="submit"/>
  </form>
  """

@app.route("/login",method="POST")
def do_login():
  username = request.forms.get("username")
  password = request.forms.get("password")
  print(username,password)
  if username and password:
    return "<p>login success</p>"
  else:
    return "<p>login failure</p>"

1.2動態(tài)路由

@app.route('/say/<name>')
def callback(name):
  return template('<b>Hello {{name}}</b>!')
 
@app.route('/say/<id:int>')
def callback(id):
  return template('<b>Hello {{id}}</b>!')
 
@app.route('/say/<name:re:[a-z]+>')
def callback(name):
  return template('<b>Hello {{name}}</b>!')
 
@app.route('/static/<path:path>')
def callback(path):
  return static_file(path, root='static')

1.3請求方法路由

@app.route('/hello/', method='POST')  # 等同于@app.post('/hello/')
def index():
  ...
 
@app.get('/hello/')  # 等同于@app.route('/hello/',method='GET')
def index():
  ...
 
@app.post('/hello/')  # 等同于@app.route('/hello/',method='POST')
def index():
  ...
 
@app.put('/hello/') # 等同于@app.route('/hello/',method='PUT')
def index():
  ...
 
@app.delete('/hello/') 
def index():
  ...

1.4二級路由

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle

app01 = Bottle()

@app01.route('/hello/', method='GET')
def index():
  return template('<b>App01</b>!')
app01.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle

app02 = Bottle()
@app02.route('/hello/', method='GET')
def index():
  return template('<b>App02</b>!')
app02.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle
from bottle import static_file
app = Bottle()
 
@app.route('/hello/')
def index():
  return template('<b>Root {{name}}</b>!', name="bottle")
 
from root_dir import app01
from root_dir import app02
 
app.mount('app01', app01.app01)
app.mount('app02', app02.app02)
 
app.run(host='localhost', port=8888)

1.5靜態(tài)文件映射,static_file()函數(shù)用于響應(yīng)靜態(tài)文件的請求

# 靜態(tài)文件映射,static_file()函數(shù)用于響應(yīng)靜態(tài)文件 的請求
@app.route("/static/<filename:re:.*\.jpg>")
def send_image(filename):
  return static_file(filename, root=os.getcwd(), mimetype="image/jpg")

@app.route("/static/<filename:path>")  # 可匹配路徑
def send_image(filename):
  return static_file(filename, root=os.getcwd(), mimetype="image/jpg")

# 強制下載
@app.route("/static/<filename:path>")  # 可匹配路徑
def download(filename):
  return static_file(filename, root=os.getcwd(), download=filename)

1.6使用error()函數(shù)自定義錯誤頁面

@app.error(404)
def error404(error):
return "我找不到目標(biāo)了,我發(fā)生錯誤了"

1.7HTTP錯誤和重定向

abort()函數(shù)是生成HTTP錯誤的頁面的一個捷徑

@app.route("/restricted")
def restricted()
  abort(401,"Sorry, access denied")
# 將url重定向到其他url,可以在location中設(shè)置新的url,接著返回一個303 # redirect()函數(shù)可以幫助我們做這件事

@app.route("/wrong/url")
def wrong()
  redirect("/right/url")

其他異常

除了HTTPResponse或者HTTPError以外的其他異常,都會導(dǎo)致500錯誤,因此不會造成WSGI服務(wù)器崩潰

將bottle.app().catchall的值設(shè)為False來關(guān)閉這種行為,以便在中間件中處理異常

2.cookies

@app.route("/login", method="POST")
def do_login():
  username = request.forms.get("username")
  password = request.forms.get("password")
  print(username, password)
  if username and password:
    response.set_cookie("name",username, secret= 'some-secret-key')  # 設(shè)置cookie
    return "<p>login success</p>"
  else:
    return "<p>login failure</p>"
@app.route("/static/<filename:re:.*\.jpg>")
def send_image(filename):
  username = request.get_cookie("name", secret= 'some-secret-key')  # 獲取cookie
  if username:
    return static_file(filename, root=os.getcwd(), mimetype="image/jpg")
  else:
    return "verify failed"

bottle就的 set_cookie 的默認(rèn) path 是當(dāng)前路徑,也就是說,在這個頁面上存入的 cookie 在別的頁面通常是取不到的,不熟悉這點的人幾乎都要栽在這里。而且更坑的是:set_cookie 有 path 參數(shù)可以指定 path ,但 get_cookie 卻沒有這個 path 參數(shù)可選——也就是說,你即使設(shè)置了其它 path ,如果 get_cookie 的時候不是剛好在那個 path 下的話,也取不到……

解決方法:把所有的 cookie 都放到"/"下面,至少目前用下來感覺沒問題。

注:request.query 或 request.forms 都是一個 FormDict 類型,

其特點是:當(dāng)以屬性方式訪問數(shù)據(jù)時——如 request.query.name,返回的結(jié)果是 unicode ,當(dāng)以字典試訪問數(shù)據(jù)時,如 :request.query['name']或者request.query.get("name"),則返回的結(jié)果是原編碼字符串

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python讀取LMDB中圖像的方法

    python讀取LMDB中圖像的方法

    這篇文章主要為大家詳細介紹了python讀取LMDB中圖像的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python通過正則表達式選取callback的方法

    Python通過正則表達式選取callback的方法

    這篇文章主要介紹了Python通過正則表達式選取callback的方法,涉及Python正則表達式及回調(diào)函數(shù)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • TensorFlow中tf.batch_matmul()的用法

    TensorFlow中tf.batch_matmul()的用法

    這篇文章主要介紹了TensorFlow中tf.batch_matmul()的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python3.9.1中使用split()的處理方法(推薦)

    Python3.9.1中使用split()的處理方法(推薦)

    這篇文章主要介紹了Python3.9.1中使用split()的處理方法(推薦),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • python技能之?dāng)?shù)據(jù)導(dǎo)出excel的實例代碼

    python技能之?dāng)?shù)據(jù)導(dǎo)出excel的實例代碼

    本篇文章主要介紹了python技能之導(dǎo)出excel的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • python中eval函數(shù)使用與異常處理詳解

    python中eval函數(shù)使用與異常處理詳解

    這篇文章主要給大家介紹了關(guān)于python中eval函數(shù)使用與異常處理的相關(guān)資料,eval()函數(shù)用來執(zhí)行一個字符串表達式,并返回表達式的值,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-04-04
  • 關(guān)于python之字典的嵌套,遞歸調(diào)用方法

    關(guān)于python之字典的嵌套,遞歸調(diào)用方法

    今天小編就為大家分享一篇關(guān)于python之字典的嵌套,遞歸調(diào)用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python 中單例模式的實現(xiàn)方法

    Python 中單例模式的實現(xiàn)方法

    這篇文章主要介紹了Python 中單例模式的實現(xiàn)方法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以學(xué)習(xí)一下下面文章詳細內(nèi)容
    2022-08-08
  • Pandas之DataFrame對象的列和索引之間的轉(zhuǎn)化

    Pandas之DataFrame對象的列和索引之間的轉(zhuǎn)化

    這篇文章主要介紹了Pandas之DataFrame對象的列和索引之間的轉(zhuǎn)化,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Django+Xadmin構(gòu)建項目的方法步驟

    Django+Xadmin構(gòu)建項目的方法步驟

    這篇文章主要介紹了Django+Xadmin構(gòu)建項目的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03

最新評論