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


WEB框架功能分析

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

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

WSGI用來接收請求,然后封裝請求。對於Django來說都封裝到了request里面。

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

回顧下Django的生命周期

Python的WEB框架們

一、Bottle

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

1、安裝Bottle

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

2、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,
}

3、使用bottle框架

導入模塊后,聽過定位可以查看到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)

4、路由系統

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

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

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

4.1、靜態路由

@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.2、動態路由(支持正則)

舉例來說:

@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.3、請求方法路由

在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.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>!')

5、模板系統

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.2、語法

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

    <h1>2、單行Python代碼</h1>
        % s1 = "hello"
        {{ s1 }}
    #'%s1 這里設置一個變量,我們可以在html調用和python類似'

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

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

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

</body>
</html>

5.3、函數

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.4、擴展自定義函數

<!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)

6、公共組件

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

【接收用戶請求】

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

【響應相關內容】

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

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

6.1、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,並且也沒有模板引擎!

1、安裝

pip install Flask

2、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框架

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

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

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

 

那么,這些代碼是什么意思呢?

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

4、路由系統

  • @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、模板

5.1、模板使用

Flask使用的是Jinja2模板,所以其語法和Django無差別

5.2、自定義模板語言

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 模板引擎的威力。

 

6、公共組件

對於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.2、響應

當用戶請求被開發人員的邏輯處理完成之后,會將結果發送給用戶瀏覽器,那么就需要對請求做出相應的響應。

字符串

@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.3、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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM