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/

相关文章
相关标签/搜索