Python之路【第二十篇】其餘WEB框架

WEB框架功能分析

WEB框架本質上,就是一個SOCKET Serverhtml

WEB框架前面有WSGI或者是本身寫的SOCKET,而後交給URL路由系統處理,而後交給某個函數或某個類,而後在模板裏拿到模板而後模板和數據進行混合而後返回給用戶!python

WSGI用來接收請求,而後封裝請求。對於Django來講都封裝到了request裏面。正則表達式

把整個WEB框架的環整明白了在學習其餘的WEB框架就簡單多了。數據庫

回顧下Django的生命週期flask

Python的WEB框架們

1、Bottlesegmentfault

Bottle是一個快速、簡潔、輕量級的基於WSIG的微型Web框架,此框架只由一個 .py 文件,除了Python的標準庫外,其不依賴任何其餘模塊。後端

一、安裝Bottle瀏覽器

pip install bottle
easy_install bottle
apt-get install python-bottle
wget http://bottlepy.org/bottle.py

二、Bottle框架大體能夠分爲如下部分:服務器

  • 路由系統,將不一樣請求交由指定函數處理
  • 模板系統,將模板中的特殊語法渲染成字符串,值得一說的是Bottle的模板引擎能夠任意指定:Bottle內置模板、makojinja2cheetah
  • 公共組件,用於提供處理請求相關的信息,如:表單數據、cookies、請求頭等
  • 服務,Bottle默認支持多種基於WSGI的服務,如:
複製代碼
server_names = {
    'cgi': CGIServer,
    'flup': FlupFCGIServer,
    'wsgiref': WSGIRefServer,
    'waitress': WaitressServer,
    'cherrypy': CherryPyServer,
    'paste': PasteServer,
    'fapws3': FapwsServer,
    'tornado': TornadoServer,
    'gae': AppEngineServer,
    'twisted': TwistedServer,
    'diesel': DieselServer,
    'meinheld': MeinheldServer,
    'gunicorn': GunicornServer,
    'eventlet': EventletServer,
    'gevent': GeventServer,
    'geventSocketIO':GeventSocketIOServer,
    'rocket': RocketServer,
    'bjoern' : BjoernServer,
    'auto': AutoServer,
}
複製代碼

三、使用bottle框架cookie

導入模塊後,聽過定位能夠查看到Bottle模塊自己就一個文件,咱們把這一個文件導入了就可使用了,因此他提供的功能是有限的。

對於bottle他本身自己沒有實現SOCKET因此他是借用其餘WSGI,而且他自己也沒有模板引擎!

框架若是要運行最起碼須要WSGI、模板引擎

 

自己Bottle不以來任何Python模塊他只以來Python版本庫!只有在運行的時候才須要以來其餘的Python模塊。

複製代碼
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)
複製代碼

四、路由系統

路由系統是的url對應指定函數,當用戶請求某個url時,就由指定函數處理當前請求,對於Bottle的路由系統能夠分爲一下幾類:

  • 靜態路由
  • 動態路由
  • 請求方法路由
  • 二級路由

對於bottle的路由來講,指定的路由對應指定的函數

4.一、靜態路由

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

而且能夠綁定多個裝飾器如代碼(當你訪問hello的時候回執行下面的函數,當你訪問index的時候也會執行下面的index函數)

複製代碼
@root.route('/index/')
@root.route('/hello/')
def index():
    #return "Hello World"
    return template('<b style="background-color:red">Hello {{name}}</b>!', name="Tim")

root.run(host='localhost', port=8080)
複製代碼

4.二、動態路由(支持正則)

舉例來講:

@root.route('/wiki/<pagename>')
def callback(pagename):
    ...

當請求過來以後@root.route('/wiki/<pagename>') 這個pagename值就會自動把值賦值給def callback(pagename):的參數了!

 

複製代碼
@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')
複製代碼

單獨說下:@root.route('/static/<path:path>'),正常狀況下URL的設置如:/hello/那麼這個URL就結束了,若是

 

4.三、請求方法路由

在http訪問請求中有不少請求方法好比get、post、delete等

若是使用了@root.get就表示這個裝飾器下的函數只接收get請求!

複製代碼
@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():
    ...
複製代碼

4.四、二級路由

就和Django中的多個APP把不一樣app下的請求轉發到不一樣的app下面進行處理同樣!

index

複製代碼
#!/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)
複製代碼

app01&app02

複製代碼
#!/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>!')
複製代碼
複製代碼
#!/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>!')
複製代碼

五、模板系統

Bottle自己有本身的模板渲染語言的,可是他也可使用:makojinja2cheetah等!

bottle本身的模板語言例子:

html

複製代碼
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>{{name}}</h1>
</body>
</html>
複製代碼

index.py

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

5.二、語法

複製代碼
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>一、單值</h1>
      {{name}}
    #應用後端返回的數據

    <h1>二、單行Python代碼</h1>
        % s1 = "hello"
        {{ s1 }}
    #'%s1 這裏設置一個變量,咱們能夠在html調用和python相似'

    <h1>三、Python代碼塊</h1>
    <%
        # python塊代碼
        name = name.title().strip()
        if name == "shuaige":
            name="luotianshuai"
    %>

    <h1>四、Python、Html混合</h1>

    % if True:
        <span>{{name}}</span>
    % end
    <ul>
      % for item in name:
        <li>{{item}}</li>
      % end
    </ul>

</body>
</html>
複製代碼

5.三、函數

include(sub_template, **variables)

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

rebase(name, **variables)

複製代碼
<html>
<head>
  <title>{{title or 'No title'}}</title>
</head>
<body>
  {{!base}}
</body>
</html>
複製代碼
# 導入母版
 
% rebase('base.tpl', title='Page Title')
<p>Page Content ...</p>
defined(name) #檢查當前變量是否已經被定義,已定義True,未定義False
get(name, default=None) #獲取某個變量的值,不存在時可設置默認值
setdefault(name, default) #若是變量不存在時,爲變量設置默認值

5.四、擴展自定義函數

複製代碼
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>自定義函數</h1>
    {{ shuaige() }}

</body>
</html>
複製代碼

index.py

複製代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle,SimpleTemplate
root = Bottle()


def custom():
    return '123123'


@root.route('/hello/')
def index():
    # 默認狀況下去目錄:['./', './views/']中尋找模板文件 hello_template.html
    # 配置在 bottle.TEMPLATE_PATH 中
    return template('hello_template.html', name='tianshuai', shuaige=custom)

root.run(host='localhost', port=8080)
複製代碼

六、公共組件

因爲Web框架就是用來【接收用戶請求】-> 【處理用戶請求】-> 【響應相關內容】,對於具體如何處理用戶請求,開發人員根據用戶請求來進行處理,而對於接收用戶請求和相應相關的內容均交給框架自己來處理,其處理完成以後將產出交給開發人員和用戶。

【接收用戶請求】

當框架接收到用戶請求以後,將請求信息封裝在Bottle的request中,以供開發人員使用

【響應相關內容】

當開發人員的代碼處理完用戶請求以後,會將其執行內容相應給用戶,相應的內容會封裝在Bottle的response中,而後再由框架將內容返回給用戶

因此,公共組件本質其實就是爲開發人員提供接口,使其可以獲取用戶信息並配置響應內容。

6.一、request

Bottle中的request實際上是一個LocalReqeust對象,其中封裝了用戶請求的相關信息:

 

複製代碼
request.headers
#請求頭信息,能夠經過請求頭信息來獲取相關客戶端的信息
 
request.query
#get請求信息,若是用戶訪問時這樣的:http://127.0.0.1:8000/?page=123就必須使用request.query  使用GET方法是沒法取到信息的
 
request.forms
#post請求信息
 
request.files
#上傳文件信息
 
request.params
#get和post請求信息,他是GET和POST的總和,其實他內部調用了request.get request.forms
 
request.GET
#get請求信息
 
request.POST
#post和上傳信息,上傳文件信息,和post信息
 
request.cookies
#cookie信息
     
request.environ
#環境相關相關,若是上面的這些請求信息沒有知足你的需求,就在這裏找!
複製代碼

Flask框架

Flask相對於bottle來講,他要優於bottle相對於bottle來講,Flask有不少插件供其使用!

Flask是一個基於Python開發而且依賴jinja2模板和Werkzeug WSGI服務的一個微型框架,對於Werkzeug本質是Socket服務端,其用於接收http請求並對請求進行預處理,而後觸發Flask框架,開發人員基於Flask框架提供的功能對請求進行相應的處理,並返回給用戶,若是要返回給用戶複雜的內容時,須要藉助jinja2模板來實現對模板的處理,即:將模板和數據進行渲染,將渲染後的字符串返回給用戶瀏覽器。

「微」(micro) 並不表示你須要把整個 Web 應用塞進單個 Python 文件(雖然確實能夠 ),也不意味着 Flask 在功能上有所欠缺。微框架中的「微」意味着 Flask 旨在保持核心簡單而易於擴展。Flask 不會替你作出太多決策——好比使用何種數據庫。而那些 Flask 所選擇的——好比使用何種模板引擎——則很容易替換。除此以外的一切都由可由你掌握。如此,Flask 能夠與您珠聯璧合。

默認狀況下,Flask 不包含數據庫抽象層、表單驗證,或是其它任何已有多種庫能夠勝任的功能。然而,Flask 支持用擴展來給應用添加這些功能,如同是 Flask 自己實現的同樣。衆多的擴展提供了數據庫集成、表單驗證、上傳處理、各類各樣的開放認證技術等功能。Flask 也許是「微小」的,但它已準備好在需求繁雜的生產環境中投入使用。

flask它本身沒有Socket也沒有WSGI,而且也沒有模板引擎!

一、安裝

pip install Flask

二、werkzeug

複製代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from werkzeug.wrappers import Request, Response

@Request.application
def hello(request):
    return Response('Hello World!')

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('localhost', 4000, hello)
複製代碼

上面和bottle同樣經過werkzeug封裝以後而後返回給Flask框架

三、第一個Flask程序(仔細看幾大塊和bottle相似)

複製代碼
from flask import Flask
app = Flask(__name__)
 
@app.route("/")
def hello():
    return "Hello World!"
 
if __name__ == "__main__":
    app.run()
複製代碼

上面的代碼是什麼意思呢?

 

複製代碼
那麼,這些代碼是什麼意思呢?

1、首先咱們導入了 Flask 類。這個類的實例將會成爲咱們的 WSGI 應用。
二、接着咱們建立了這個類的實例。第一個參數是應用模塊或者包的名稱。若是你使用一個 單一模塊(就像本例),那麼應當使用 __name__ ,由於名稱會根據這個模塊是按 應用方式使用仍是做爲一個模塊導入而發生變化(多是 '__main__' ,也多是 實際導入的名稱)。這個參數是必需的,這樣 Flask 就能夠知道在哪裏找到模板和 靜態文件等東西。更多內容詳見 Flask 文檔。
3、而後咱們使用 route() 裝飾器來告訴 Flask 觸發函數的 URL 。
4、函數名稱可用於生成相關聯的 URL ,並返回須要在用戶瀏覽器中顯示的信息。
五、最後,使用 run() 函數來運行本地服務器和咱們的應用。 if __name__ == '__main__': 確保服務器只會在使用 Python 解釋器運行代碼的 狀況下運行,而不會在做爲模塊導入時運行。
複製代碼

四、路由系統

  • @app.route('/user/<username>')
  • @app.route('/post/<int:post_id>')
  • @app.route('/post/<float:post_id>')
  • @app.route('/post/<path:path>')
  • @app.route('/login', methods=['GET', 'POST'])

首先看這裏的flask和bottle的區別,首先bottle這裏的參數只能是一個方法,可是flask和傳多個方法!而且flask默認是不支持正則表達式的!(注:對於Flask默認不支持直接寫正則表達式的路由,不過能夠經過自定義來實現,見:https://segmentfault.com/q/1010000000125259)

經常使用路由系統有如下五種,全部的路由系統都是基於一下對應關係來處理:

複製代碼
DEFAULT_CONVERTERS = {
    'default':          UnicodeConverter,
    'string':           UnicodeConverter,
    'any':              AnyConverter,
    'path':             PathConverter,
    'int':              IntegerConverter,
    'float':            FloatConverter,
    'uuid':             UUIDConverter,
}
複製代碼

五、模板

5.一、模板使用

Flask使用的是Jinja2模板,因此其語法和Django無差異

5.二、自定義模板語言

Flask中自定義模板方法的方式和Bottle類似,建立一個函數並經過參數的形式傳入render_template,如:

複製代碼
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>自定義函數</h1>
    {{ww()|safe}}

</body>
</html>
複製代碼
複製代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask,render_template
app = Flask(__name__)
 
 
def shuaige():
    return '<h1>shuaige</h1>'
 
@app.route('/login', methods=['GET', 'POST'])
def login():
    return render_template('login.html', ww=shuaige)
 
app.run()
複製代碼

 

複製代碼
Flask 會在 templates 文件夾內尋找模板。所以,若是你的應用是一個模塊,那麼模板 文件夾應該在模塊旁邊;若是是一個包,那麼就應該在包裏面:

情形 1: 一個模塊:

/application.py
/templates
    /hello.html
情形 2: 一個包:

/application
    /__init__.py
    /templates
        /hello.html
你能夠充分使用 Jinja2 模板引擎的威力。
複製代碼

 

六、公共組件

對於Http請求,Flask會講請求信息封裝在request中(werkzeug.wrappers.BaseRequest),提供的以下經常使用方法和字段以供使用:

複製代碼
request.method
request.args
request.form
request.values
request.files
request.cookies
request.headers
request.path
request.full_path
request.script_root
request.url
request.base_url
request.url_root
request.host_url
request.host
複製代碼

表單處理

複製代碼
@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'],
                       request.form['password']):
            return log_the_user_in(request.form['username'])
        else:
            error = 'Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return render_template('login.html', error=error)
複製代碼

上傳文件

複製代碼
from flask import request
from werkzeug import secure_filename

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['the_file']
        f.save('/var/www/uploads/' + secure_filename(f.filename))
    ...
複製代碼

cookie操做

複製代碼
from flask import request

@app.route('/setcookie/')
def index():
    username = request.cookies.get('username')
    # use cookies.get(key) instead of cookies[key] to not get a
    # KeyError if the cookie is missing.




from flask import make_response

@app.route('/getcookie')
def index():
    resp = make_response(render_template(...))
    resp.set_cookie('username', 'the username')
    return resp
複製代碼

6.二、響應

當用戶請求被開發人員的邏輯處理完成以後,會將結果發送給用戶瀏覽器,那麼就須要對請求作出相應的響應。

字符串

@app.route('/index/', methods=['GET', 'POST'])
def index():
    return "index"

模板引擎

複製代碼
from flask import Flask,render_template,request
app = Flask(__name__)
 
@app.route('/index/', methods=['GET', 'POST'])
def index():
    return render_template("index.html")
 
app.run()
複製代碼

重定向

複製代碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask, redirect, url_for
app = Flask(__name__)
 
@app.route('/index/', methods=['GET', 'POST'])
def index():
    # return redirect('/login/')
    return redirect(url_for('login'))
 
@app.route('/login/', methods=['GET', 'POST'])
def login():
    return "LOGIN"
 
app.run()
複製代碼

錯誤頁面

  --指定URL,簡單錯誤

複製代碼
from flask import Flask, abort, render_template
app = Flask(__name__)

@app.route('/e1/', methods=['GET', 'POST'])
def index():
    abort(404, 'Nothing')
app.run()
複製代碼
複製代碼
from flask import Flask, abort, render_template
app = Flask(__name__)
 
@app.route('/index/', methods=['GET', 'POST'])
def index():
    return "OK"
 
@app.errorhandler(404)
def page_not_found(error):
    return render_template('page_not_found.html'), 404
 
app.run()
複製代碼

設置相應信息

使用make_response能夠對相應的內容進行操做

複製代碼
from flask import Flask, abort, render_template,make_response
app = Flask(__name__)
 
@app.route('/index/', methods=['GET', 'POST'])
def index():
    response = make_response(render_template('index.html'))
    # response是flask.wrappers.Response類型
    # response.delete_cookie
    # response.set_cookie
    # response.headers['X-Something'] = 'A value'
    return response
 
app.run()
複製代碼

6.三、Session

除請求對象以外,還有一個 session 對象。它容許你在不一樣請求間存儲特定用戶的信息。它是在 Cookies 的基礎上實現的,而且對 Cookies 進行密鑰簽名要使用會話,你須要設置一個密鑰。

  • 設置:session['username'] = 'xxx'

  • 刪除:session.pop('username', None)
複製代碼
from flask import Flask, session, redirect, url_for, escape, request
 
app = Flask(__name__)
 
@app.route('/')
def index():
    if 'username' in session:
        return 'Logged in as %s' % escape(session['username'])
    return 'You are not logged in'
 
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    return '''
        <form action="" method="post">
            <p><input type=text name=username>
            <p><input type=submit value=Login>
        </form>
    '''
 
@app.route('/logout')
def logout():
    # remove the username from the session if it's there
    session.pop('username', None)
    return redirect(url_for('index'))
 
# set the secret key.  keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
複製代碼

更多參考:http://www.cnblogs.com/wupeiqi/articles/5341480.html

很是不錯的Flask網站:https://dormousehole.readthedocs.org/en/latest/

相關文章
相關標籤/搜索