Python的WEB框架html
Bottle是一個快速、簡潔、輕量級的基於WSIG的微型Web框架,此框架只由一個 .py 文件,除了Python的標準庫外,其不依賴任何其餘模塊。python
1
2
3
4
|
pip install bottle
easy_install bottle
apt
-
get install python
-
bottle
wget http:
/
/
bottlepy.org
/
bottle.py
|
Bottle框架大體能夠分爲如下部分:正則表達式
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, }
框架的基本使用數據庫
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
)
|
1、路由系統flask
路由系統是的url對應指定函數,當用戶請求某個url時,就由指定函數處理當前請求,對於Bottle的路由系統能夠分爲一下幾類:segmentfault
一、靜態路由瀏覽器
1
2
3
|
@root
.route(
'/hello/'
)
def
index():
return
template(
'<b>Hello {{name}}</b>!'
, name
=
"Alex"
)
|
二、動態路由cookie
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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'
)
|
三、請求方法路由session
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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():
...
|
四、二級路由app
#!/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/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
)
|
2、模板系統
模板系統用於將Html和自定的值二者進行渲染,從而獲得字符串,而後將該字符串返回給客戶端。咱們知道在Bottle中可使用 內置模板系統、mako、jinja2、cheetah等,之內置模板系統爲例:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>{{name}}</h1> </body> </html> hello_template.tpl
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/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
=
'alex'
)
root.run(host
=
'localhost'
, port
=
8080
)
|
一、語法
<h1>一、單值</h1> {{name}} <h1>二、單行Python代碼</h1> % s1 = "hello" <h1>三、Python代碼塊</h1> <% # A block of python code name = name.title().strip() if name == "Alex": name="seven" %> <h1>四、Python、Html混合</h1> % if True: <span>{{name}}</span> % end <ul> % for item in name: <li>{{item}}</li> % end </ul>
二、函數
include(sub_template, **variables)
1
2
3
4
5
|
# 導入其餘模板文件
%
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> base.tpl
1
2
3
4
|
# 導入母版
%
rebase(
'base.tpl'
, title
=
'Page Title'
)
<p>Page Content ...<
/
p>
|
defined(name)
1
|
# 檢查當前變量是否已經被定義,已定義True,未定義False
|
get(name, default=None)
1
|
# 獲取某個變量的值,不存在時可設置默認值
|
setdefault(name, default)
1
|
# 若是變量不存在時,爲變量設置默認值
|
擴展:自定義函數
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>自定義函數</h1> {{ wupeiqi() }} </body> </html> hello_template.tpl
#!/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='alex', wupeiqi=custom) root.run(host='localhost', port=8080) main.py
注:變量或函數前添加 【 ! 】,則會關閉轉義的功能
3、公共組件
因爲Web框架就是用來【接收用戶請求】-> 【處理用戶請求】-> 【響應相關內容】,對於具體如何處理用戶請求,開發人員根據用戶請求來進行處理,而對於接收用戶請求和相應相關的內容均交給框架自己來處理,其處理完成以後將產出交給開發人員和用戶。
【接收用戶請求】
當框架接收到用戶請求以後,將請求信息封裝在Bottle的request中,以供開發人員使用
【響應相關內容】
當開發人員的代碼處理完用戶請求以後,會將其執行內容相應給用戶,相應的內容會封裝在Bottle的response中,而後再由框架將內容返回給用戶
因此,公共組件本質其實就是爲開發人員提供接口,使其可以獲取用戶信息並配置響應內容。
一、request
Bottle中的request實際上是一個LocalReqeust對象,其中封裝了用戶請求的相關信息:
request.headers 請求頭信息 request.query get請求信息 request.forms post請求信息 request.files 上傳文件信息 request.params get和post請求信息 request.GET get請求信息 request.POST post和上傳信息 request.cookies cookie信息 request.environ 環境相關相關
二、response
Bottle中的request實際上是一個LocalResponse對象,其中框架即將返回給用戶的相關信息:
response response.status_line 狀態行 response.status_code 狀態碼 response.headers 響應頭 response.charset 編碼 response.set_cookie 在瀏覽器上設置cookie response.delete_cookie 在瀏覽器上刪除cookie
實例:
from bottle import route, request @route('/login') def login(): return ''' <form action="/login" method="post"> Username: <input name="username" type="text" /> Password: <input name="password" type="password" /> <input value="Login" type="submit" /> </form> ''' @route('/login', method='POST') def do_login(): username = request.forms.get('username') password = request.forms.get('password') if check_login(username, password): return "<p>Your login information was correct.</p>" else: return "<p>Login failed.</p>" 基本Form請求
<form action="/upload" method="post" enctype="multipart/form-data"> Category: <input type="text" name="category" /> Select a file: <input type="file" name="upload" /> <input type="submit" value="Start upload" /> </form> @route('/upload', method='POST') def do_upload(): category = request.forms.get('category') upload = request.files.get('upload') name, ext = os.path.splitext(upload.filename) if ext not in ('.png','.jpg','.jpeg'): return 'File extension not allowed.' save_path = get_save_path_for_category(category) upload.save(save_path) # appends upload.filename automatically return 'OK' 上傳文件
4、服務
對於Bottle框架其自己未實現相似於Tornado本身基於socket實現Web服務,因此必須依賴WSGI,默認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, } WSGI
使用時,只需在主app執行run方法時指定參數便可:
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"
# 默認server ='wsgiref'
root.run(host
=
'localhost'
, port
=
8080
, server
=
'wsgiref'
)
|
默認server="wsgiref",即:使用Python內置模塊wsgiref,若是想要使用其餘時,則須要首先安裝相關類庫,而後才能使用。如:
# 若是使用Tornado的服務,則須要首先安裝tornado才能使用 class TornadoServer(ServerAdapter): """ The super hyped asynchronous server by facebook. Untested. """ def run(self, handler): # pragma: no cover # 導入Tornado相關模塊 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() bottle.py源碼
PS:以上WSGI中提供了19種,若是想要使期支持其餘服務,則須要擴展Bottle源碼來自定義一個ServerAdapter
更多參見:http://www.bottlepy.org/docs/dev/index.html
Flask是一個基於Python開發而且依賴jinja2模板和Werkzeug WSGI服務的一個微型框架,對於Werkzeug本質是Socket服務端,其用於接收http請求並對請求進行預處理,而後觸發Flask框架,開發人員基於Flask框架提供的功能對請求進行相應的處理,並返回給用戶,若是要返回給用戶複雜的內容時,須要藉助jinja2模板來實現對模板的處理,即:將模板和數據進行渲染,將渲染後的字符串返回給用戶瀏覽器。
「微」(micro) 並不表示你須要把整個 Web 應用塞進單個 Python 文件(雖然確實能夠 ),也不意味着 Flask 在功能上有所欠缺。微框架中的「微」意味着 Flask 旨在保持核心簡單而易於擴展。Flask 不會替你作出太多決策——好比使用何種數據庫。而那些 Flask 所選擇的——好比使用何種模板引擎——則很容易替換。除此以外的一切都由可由你掌握。如此,Flask 能夠與您珠聯璧合。
默認狀況下,Flask 不包含數據庫抽象層、表單驗證,或是其它任何已有多種庫能夠勝任的功能。然而,Flask 支持用擴展來給應用添加這些功能,如同是 Flask 自己實現的同樣。衆多的擴展提供了數據庫集成、表單驗證、上傳處理、各類各樣的開放認證技術等功能。Flask 也許是「微小」的,但它已準備好在需求繁雜的生產環境中投入使用。
安裝
1
|
pip install Flask
|
#!/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) werkzeug
1、第一次
1
2
3
4
5
6
7
8
9
|
from
flask
import
Flask
app
=
Flask(__name__)
@app
.route(
"/"
)
def
hello():
return
"Hello World!"
if
__name__
=
=
"__main__"
:
app.run()
|
2、路由系統
經常使用路由系統有以上五種,全部的路由系統都是基於一下對應關係來處理:
1
2
3
4
5
6
7
8
9
|
DEFAULT_CONVERTERS
=
{
'default'
: UnicodeConverter,
'string'
: UnicodeConverter,
'any'
: AnyConverter,
'path'
: PathConverter,
'int'
: IntegerConverter,
'float'
: FloatConverter,
'uuid'
: UUIDConverter,
}
|
注:對於Flask默認不支持直接寫正則表達式的路由,不過能夠經過自定義來實現,見:https://segmentfault.com/q/1010000000125259
3、模板
一、模板的使用
Flask使用的是Jinja2模板,因此其語法和Django無差異
二、自定義模板方法
Flask中自定義模板方法的方式和Bottle類似,建立一個函數並經過參數的形式傳入render_template,如:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>自定義函數</h1> {{ww()|safe}} </body> </html> index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
flask
import
Flask,render_template
app
=
Flask(__name__)
def
wupeiqi():
return
'<h1>Wupeiqi</h1>'
@app
.route(
'/login'
, methods
=
[
'GET'
,
'POST'
])
def
login():
return
render_template(
'login.html'
, ww
=
wupeiqi)
app.run()
|
4、公共組件
一、請求
對於Http請求,Flask會講請求信息封裝在request中(werkzeug.wrappers.BaseRequest),提供的以下經常使用方法和字段以供使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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) 表單處理Demo
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)) ... 上傳文件Demo
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 Cookie操做
二、響應
當用戶請求被開發人員的邏輯處理完成以後,會將結果發送給用戶瀏覽器,那麼就須要對請求作出相應的響應。
a.字符串
1
2
3
|
@app
.route(
'/index/'
, methods
=
[
'GET'
,
'POST'
])
def
index():
return
"index"
|
b.模板引擎
1
2
3
4
5
6
7
8
|
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()
|
c.重定向
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/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()
|
d.錯誤頁面
from flask import Flask, abort, render_template app = Flask(__name__) @app.route('/e1/', methods=['GET', 'POST']) def index(): abort(404, 'Nothing') app.run() 指定URL,簡單錯誤
1
2
3
4
5
6
7
8
9
10
11
12
|
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()
|
e.設置相應信息
使用make_response能夠對相應的內容進行操做
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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()
|
三、Session
除請求對象以外,還有一個 session 對象。它容許你在不一樣請求間存儲特定用戶的信息。它是在 Cookies 的基礎上實現的,而且對 Cookies 進行密鑰簽名要使用會話,你須要設置一個密鑰。
設置:session['username'] = 'xxx'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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'
|
4.message
message是一個基於Session實現的用於保存數據的集合,其特色是:使用一次就刪除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from
flask
import
Flask, flash, redirect, render_template, request
app
=
Flask(__name__)
app.secret_key
=
'some_secret'
@app
.route(
'/'
)
def
index1():
return
render_template(
'index.html'
)
@app
.route(
'/set'
)
def
index2():
v
=
request.args.get(
'p'
)
flash(v)
return
'ok'
if
__name__
=
=
"__main__"
:
app.run()
|
5.中間件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
from
flask
import
Flask, flash, redirect, render_template, request
app
=
Flask(__name__)
app.secret_key
=
'some_secret'
@app
.route(
'/'
)
def
index1():
return
render_template(
'index.html'
)
@app
.route(
'/set'
)
def
index2():
v
=
request.args.get(
'p'
)
flash(v)
return
'ok'
class
MiddleWare:
def
__init__(
self
,wsgi_app):
self
.wsgi_app
=
wsgi_app
def
__call__(
self
,
*
args,
*
*
kwargs):
return
self
.wsgi_app(
*
args,
*
*
kwargs)
if
__name__
=
=
"__main__"
:
app.wsgi_app
=
MiddleWare(app.wsgi_app)
app.run(port
=
9999
)
|
Flask還有衆多其餘功能,更多參見: http://docs.jinkan.org/docs/flask/ http://flask.pocoo.org/