最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python輕量級(jí)web框架bottle使用方法解析

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

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

#!/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對(duì)應(yīng)指定函數(shù),當(dāng)用戶(hù)請(qǐng)求某個(gè)url時(shí),就由指定函數(shù)處理當(dāng)前請(qǐng)求,對(duì)于Bottle的路由系統(tǒng)可以分為一下幾類(lèi):

  • 靜態(tài)路由
  • 動(dòng)態(tài)路由
  • 請(qǐng)求方法路由
  • 二級(jí)路由

1.1靜態(tài)路由

@app.route("/login") # 默認(rèn)為get請(qǐng)求
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動(dòng)態(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請(qǐng)求方法路由

@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二級(jí)路由

#!/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)文件的請(qǐng)求

# 靜態(tài)文件映射,static_file()函數(shù)用于響應(yīng)靜態(tài)文件 的請(qǐng)求
@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")

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

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

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

1.7HTTP錯(cuò)誤和重定向

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

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

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

其他異常

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

將bottle.app().catchall的值設(shè)為False來(lái)關(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)前路徑,也就是說(shuō),在這個(gè)頁(yè)面上存入的 cookie 在別的頁(yè)面通常是取不到的,不熟悉這點(diǎn)的人幾乎都要栽在這里。而且更坑的是:set_cookie 有 path 參數(shù)可以指定 path ,但 get_cookie 卻沒(méi)有這個(gè) path 參數(shù)可選——也就是說(shuō),你即使設(shè)置了其它 path ,如果 get_cookie 的時(shí)候不是剛好在那個(gè) path 下的話(huà),也取不到……

解決方法:把所有的 cookie 都放到"/"下面,至少目前用下來(lái)感覺(jué)沒(méi)問(wèn)題。

注:request.query 或 request.forms 都是一個(gè) FormDict 類(lèi)型,

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

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

相關(guān)文章

  • Python中正則表達(dá)式對(duì)單個(gè)字符,多個(gè)字符和匹配邊界等使用

    Python中正則表達(dá)式對(duì)單個(gè)字符,多個(gè)字符和匹配邊界等使用

    這篇文章主要介紹了Python中正則表達(dá)式對(duì)單個(gè)字符,多個(gè)字符和匹配邊界等使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python入門(mén)教程(九)Python字符串介紹

    Python入門(mén)教程(九)Python字符串介紹

    這篇文章主要介紹了Python入門(mén)教程(九)Python字符串,Python是一門(mén)非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門(mén)教程,需要的朋友可以參考下
    2023-04-04
  • Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用講解

    Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用講解

    這篇文章主要介紹了Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用,通過(guò)實(shí)例代碼給大家介紹了Python連接Redis數(shù)據(jù)庫(kù)方法,Python使用連接池連接Redis數(shù)據(jù)庫(kù)方法,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • 解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題

    解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題

    今天小編就為大家分享一篇解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作

    python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作

    這篇文章主要介紹了python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作,文章介紹內(nèi)容首先將對(duì)象轉(zhuǎn)為json字符串展開(kāi)主題詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Python?PDF轉(zhuǎn)化wolrd代碼的寫(xiě)法小結(jié)

    Python?PDF轉(zhuǎn)化wolrd代碼的寫(xiě)法小結(jié)

    將PDF文件轉(zhuǎn)換為Word文檔的過(guò)程通常需要使用一些外部庫(kù)來(lái)實(shí)現(xiàn),因?yàn)镻ython本身并不直接支持這種轉(zhuǎn)換,這篇文章主要介紹了Python?PDF轉(zhuǎn)化wolrd代碼的寫(xiě)法小結(jié),需要的朋友可以參考下
    2024-06-06
  • Python urls.py的三種配置寫(xiě)法實(shí)例詳解

    Python urls.py的三種配置寫(xiě)法實(shí)例詳解

    這篇文章主要介紹了Python urls.py的三種配置寫(xiě)法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 用基于python的appium爬取b站直播消費(fèi)記錄

    用基于python的appium爬取b站直播消費(fèi)記錄

    因工作需要,需要爬取相關(guān)數(shù)據(jù),之前是爬取網(wǎng)頁(yè)數(shù)據(jù),可以用python的requests和Selenium進(jìn)行爬取。但b站的直播消費(fèi)數(shù)據(jù)網(wǎng)頁(yè)版不能顯示,只能在手機(jī)上看到,所以就有了這篇文章。需要的朋友可以參考下
    2021-04-04
  • python如何從鍵盤(pán)獲取輸入實(shí)例

    python如何從鍵盤(pán)獲取輸入實(shí)例

    在本篇內(nèi)容中小編給各位整理的是關(guān)于python怎么從鍵盤(pán)獲取輸入的實(shí)例內(nèi)容,需要的朋友們可以參考下。
    2020-06-06
  • 給Python入門(mén)者的一些編程建議

    給Python入門(mén)者的一些編程建議

    這篇文章主要介紹了給Python入門(mén)者的一些編程建議,包括對(duì)集合初始化和GIL理解等一些需要注意的地方,需要的朋友可以參考下
    2015-06-06

最新評(píng)論

宜州市| 屯留县| 顺平县| 许昌县| 大邑县| 娱乐| 紫金县| 仙居县| 永胜县| 普洱| 保康县| 扎兰屯市| 冷水江市| 澜沧| 容城县| 延吉市| 峡江县| 高邮市| 万载县| 农安县| 准格尔旗| 辽源市| 新平| 三门县| 彩票| 偏关县| 通渭县| 顺平县| 道孚县| 清新县| 上高县| 安西县| 花莲市| 宣武区| 荔浦县| 吕梁市| 龙里县| 集安市| 合山市| 灵寿县| 金乡县|