日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

【Python之路】特別篇

 highoo 2019-03-20

Bottle

Bottle是一個(gè)快速、簡潔、輕量級(jí)的基于WSIG的微型Web框架,此框架只由一個(gè) .py 文件,除了Python的標(biāo)準(zhǔn)庫外,其不依賴任何其他模塊。

Bottle框架大致可以分為以下部分:

  • 路由系統(tǒng),將不同請(qǐng)求交由指定函數(shù)處理

  • 模板系統(tǒng),將模板中的特殊語法渲染成字符串,值得一說的是Bottle的模板引擎可以任意指定:Bottle內(nèi)置模板、mako、jinja2cheetah

  • 公共組件,用于提供處理請(qǐng)求相關(guān)的信息,如:表單數(shù)據(jù)、cookies、請(qǐng)求頭等

  • 服務(wù),Bottle默認(rèn)支持多種基于WSGI的服務(wù)

框架的基本使用:

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle
root = Bottle()
  
@root.route('/hello/')
def index():
    return "Hello World"
    # return template('<b>Hello {{name}}</b>!', name="Alex")
  
root.run(host='localhost', port=8080)

一、路由系統(tǒng)

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

  • 靜態(tài)路由

  • 動(dòng)態(tài)路由

  • 請(qǐng)求方法路由

  • 二級(jí)路由

1.靜態(tài)路由

@root.route('/hello/')
def index():
    return template('<b>Hello {{name}}</b>!', name="Alex")

2.動(dòng)態(tài)路由

復(fù)制代碼
@root.route('/wiki/<pagename>')
def callback(pagename):
    ...
 
@root.route('/object/<id:int>')
def callback(id):
    ...
 
@root.route('/show/<name:re:[a-z]+>')
def callback(name):
    ...
 
@root.route('/static/<path:path>')
def callback(path):
    return static_file(path, root='static')
復(fù)制代碼

3.請(qǐng)求方法路由

復(fù)制代碼
@root.route('/hello/', method='POST')
def index():
    ...
 
@root.get('/hello/')
def index():
    ...
 
@root.post('/hello/')
def index():
    ...
 
@root.put('/hello/')
def index():
    ...
 
@root.delete('/hello/')
def index():
    ...
復(fù)制代碼

4.二級(jí)路由

app01.py
app02.py
復(fù)制代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle
from bottle import static_file
root = Bottle()
 
@root.route('/hello/')
def index():
    return template('<b>Root {{name}}</b>!', name="Alex")
 
from framwork_bottle import app01
from framwork_bottle import app02
 
root.mount('app01', app01.app01)
root.mount('app02', app02.app02)
 
root.run(host='localhost', port=8080)
復(fù)制代碼

二、模板系統(tǒng)

模板系統(tǒng)用于將Html和自定的值兩者進(jìn)行渲染,從而得到字符串,然后將該字符串返回給客戶端。

我們知道在Bottle中可以使用 內(nèi)置模板系統(tǒng)、makojinja2、cheetah等,以內(nèi)置模板系統(tǒng)為例:

html文件
復(fù)制代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle
root = Bottle()
 
@root.route('/hello/')
def index():
    # 默認(rèn)情況下去目錄:['./', './views/']中尋找模板文件 hello_template.html
    # 配置在 bottle.TEMPLATE_PATH 中
    return template('xx.html', name='alex')
 
root.run(host='localhost', port=8080)
復(fù)制代碼

1、語法

  • 單值

  • 單行Python代碼

  • Python代碼快

  • Python、Html混合

復(fù)制代碼
<h1>1、單值</h1>
{{name}}
 
<h1>2、單行Python代碼</h1>
% s1 = "hello"
 
<h1>3、Python代碼塊</h1>
<%
    # A block of python code
    name = name.title().strip()
    if name == "Alex":
        name="seven"
%>
 
<h1>4、Python、Html混合</h1>
 
% if True:
    <span>{{name}}</span>
% end
<ul>
  % for item in name:
    <li>{{item}}</li>
  % end
</ul>
復(fù)制代碼

2、函數(shù) 

include(sub_template, **variables)

# 導(dǎo)入其他模板文件
 
% include('header.tpl', title='Page Title')
Page Content
% include('footer.tpl')

rebase(name, **variables)

base.html
# 導(dǎo)入母版
 
% rebase('base.html', title='Page Title')
<p>Page Content ...</p>

defined(name)

# 檢查當(dāng)前變量是否已經(jīng)被定義,已定義True,未定義False

get(name, default=None)

# 獲取某個(gè)變量的值,不存在時(shí)可設(shè)置默認(rèn)值

setdefault(name, default)

# 如果變量不存在時(shí),為變量設(shè)置默認(rèn)值

擴(kuò)展:自定義函數(shù)

hello.html
main.py

注:變量或函數(shù)前添加 【 ! 】,則會(huì)關(guān)閉轉(zhuǎn)義的功能

三、公共組件

由于Web框架就是用來【接收用戶請(qǐng)求】-> 【處理用戶請(qǐng)求】-> 【響應(yīng)相關(guān)內(nèi)容】,對(duì)于具體如何處理用戶請(qǐng)求,開發(fā)人員根據(jù)用戶請(qǐng)求來進(jìn)行處理,而對(duì)于接收用戶請(qǐng)求和相應(yīng)相關(guān)的內(nèi)容均交給框架本身來處理,其處理完成之后將產(chǎn)出交給開發(fā)人員和用戶。

【接收用戶請(qǐng)求】

當(dāng)框架接收到用戶請(qǐng)求之后,將請(qǐng)求信息封裝在Bottle的request中,以供開發(fā)人員使用

【響應(yīng)相關(guān)內(nèi)容】

當(dāng)開發(fā)人員的代碼處理完用戶請(qǐng)求之后,會(huì)將其執(zhí)行內(nèi)容相應(yīng)給用戶,相應(yīng)的內(nèi)容會(huì)封裝在Bottle的response中,然后再由框架將內(nèi)容返回給用戶

所以,公共組件本質(zhì)其實(shí)就是為開發(fā)人員提供接口,使其能夠獲取用戶信息并配置響應(yīng)內(nèi)容。

1、request

Bottle中的request其實(shí)是一個(gè)LocalReqeust對(duì)象,其中封裝了用戶請(qǐng)求的相關(guān)信息:

復(fù)制代碼
request.headers
    請(qǐng)求頭信息
 
request.query
    get請(qǐng)求信息
 
request.forms
    post請(qǐng)求信息
 
request.files
    上傳文件信息
 
request.params
    get和post請(qǐng)求信息
 
request.GET
    get請(qǐng)求信息
 
request.POST
    post和上傳信息
 
request.cookies
    cookie信息
     
request.environ
    環(huán)境相關(guān)相關(guān)
復(fù)制代碼

2、response

Bottle中的response其實(shí)是一個(gè)LocalResponse對(duì)象,其中框架即將返回給用戶的相關(guān)信息:

復(fù)制代碼
response
    response.status_line
        狀態(tài)行
 
    response.status_code
        狀態(tài)碼
 
    response.headers
        響應(yīng)頭
 
    response.charset
        編碼
 
    response.set_cookie
        在瀏覽器上設(shè)置cookie
         
    response.delete_cookie
        在瀏覽器上刪除cookie
復(fù)制代碼

實(shí)例:

基本Form請(qǐng)求
上傳文件

四、服務(wù)

對(duì)于Bottle框架其本身未實(shí)現(xiàn)類似于Tornado自己基于socket實(shí)現(xiàn)Web服務(wù),所以必須依賴WSGI,默認(rèn)Bottle已經(jīng)實(shí)現(xiàn)并且支持的WSGI有:

WSGI

使用時(shí),只需在主app執(zhí)行run方法時(shí)指定參數(shù)即可:

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import Bottle
root = Bottle()
  
@root.route('/hello/')
def index():
    return "Hello World"<br>
# 默認(rèn)server ='wsgiref'
root.run(host='localhost', port=8080, server='wsgiref')

默認(rèn)server="wsgiref",即:使用Python內(nèi)置模塊wsgiref,如果想要使用其他時(shí),則需要首先安裝相關(guān)類庫,然后才能使用。如:

復(fù)制代碼
# 如果使用Tornado的服務(wù),則需要首先安裝tornado才能使用

class TornadoServer(ServerAdapter):
    """ The super hyped asynchronous server by facebook. Untested. """
    def run(self, handler): # pragma: no cover
        # 導(dǎo)入Tornado相關(guān)模塊
        import tornado.wsgi, tornado.httpserver, tornado.ioloop
        container = tornado.wsgi.WSGIContainer(handler)
        server = tornado.httpserver.HTTPServer(container)
        server.listen(port=self.port,address=self.host)
        tornado.ioloop.IOLoop.instance().start()
復(fù)制代碼

PS:以上WSGI中提供了19種,如果想要使期支持其他服務(wù),則需要擴(kuò)展Bottle源碼來自定義一個(gè)ServerAdapter

更多參見:http://www./docs/dev/index.html

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多