• 基础知识


    Flask是一个精简的python web框架,没有封装很多功能,而是通过扩展,来实现相应功能    

    Flask 依赖两个外部库:Werkzeug 和 Jinja2 。 Werkzeug 是一个 WSGI(在 Web 应用和多种服务器之间的标准 Python 接口) 工具集。Jinja2 负责渲染模板。


    @app.route('/')
    def hello_world():
    return 'Hello World!'
    Flask 把request 和返回的头信息,body信息,进行了进一步的封装,看起来更接近一个函数实现一个功能,对http请求与响应进一步封装

    app = Flask(__name__)@app.route('/')def hello_world():
    导入了 Flask 类。这个类的实例将会是 WSGI 应用程序。
    route() 装饰器告诉 Flask 什么样的URL 能触发我们的函数。
    首先实例化WSGI应用程序,然后通过装饰器配置url
    @app.route('/')  将url通过WSGI的实例进行处理,也就是接受http请求
    解析http请求,响应http请求,封装好的这些功能通过装饰器进行绑定使用,更加方便简明。

    app = Flask(__name__)
    作为单独应用启动还是作为模块导入

    if __name__ == '__main__':
    app.run()
    最后我们用 run() 函数来让应用运行在本地服务器上
    run() 方法适用于启动本地的开发服务器


    外部可访问的服务器
    app.run(host='0.0.0.0')

    这会让操作系统监听所有公网 IP。


    调试模式

    如果你启用了调试支持,服务器会在代码修改后自动重新载入,并在发生错误时提供一个相当有用的调试器。
    app.run(debug=True)
    只能不用调试,不能用于生产环境
    开启调试可以得到错误信息


    路由

     route() 装饰器把一个函数绑定到对应的 URL 上。

    变量规则

    @app.route('/user/<username>')def show_user_profile(username):# show the user profile for that userreturn 'User %s' % username@app.route('/post/<int:post_id>')def show_post(post_id):# show the post with the given id, the id is an integerreturn 'Post %d' % post_id

    转换器有下面几种:

    int接受整数
    float同 int ,但是接受浮点数
    path和默认的相似,但也接受斜线

    参数看起来很简明,django是通过request.GET.get来获取url里面的参数,或者通过form表单来实现
    转换器只接受规定类型的参数,如果不是规定类型的参数,就会报错


    唯一 URL / 重定向行为
    Flask 的 URL 规则基于 Werkzeug 的路由模块。保证优雅且唯一的 URL。

    @app.route('/projects/')def projects():return 'The project page'@app.route('/about')def about():return 'The about page'
    输入projects 忘记加 / 后,会自动跳转到加了下划线的正确url上
    输入abouts加了 / 后,会报错


    构造 URL

    接受对应 URL 规则的变量部分的命名参数。
    未知变量部分会添加到 URL 末尾作为查询参数。
    url_for 第一个参数是对应函数,第二个是对应参数,不存在添加到末尾作为查询参数,存在
    作为参数传递给函数
    /login?next=//user/John%20Doe
    ...  print url_for('login', next='/')...  print url_for('profile', username='John Doe')
    >>> @app.route('/login')... def login(): pass...>>> @app.route('/user/<username>')... def profile(username): pass

    为什么你要构建 URL 而非在模板中硬编码,也就是说不是直接写出固定的url,而是通过

    url_for动态生成url_for

    这里有三个绝妙的理由:

    1. 反向构建通常比硬编码的描述性更好。更重要的是,它允许你一次性修改 URL, 而不是到处边找边改。
    2. URL 构建会转义特殊字符和 Unicode 数据,免去你很多麻烦。
    3. 如果你的应用不位于 URL 的根路径(比如,在 /myapplication 下,而不是 / ),url_for() 会妥善处理这个问题。

    HTTP 方法

    默认情况下,路由只回应 GET 请求,但是通过 route() 装饰器传递 methods 参数可以改变这个行为
    @app.route('/login', methods=['GET', 'POST'])def login():if request.method == 'POST':do_the_login()else:show_the_login_form()
    HTTP 方法(也经常被叫做“谓词”)告知服务器,客户端想对请求的页面  些什么。下面的都是非常常见的方法:
    HEAD
    浏览器告诉服务器:欲获取信息,但是只关心 消息头 .
    GET
    浏览器告知服务器:只 获取 页面上的信息并发给我。这是最常用的方法。
    POST
    浏览器告诉服务器:想在 URL 上 发布 新信息。并且,服务器必须确保 数据已存储且仅存储一次。这是 HTML 表单通常发送数据到服务器的方法。
    @app.route('/login', methods=['GET', 'POST'])def login():if request.method == 'POST':do_the_login()else:show_the_login_form()


    静态文件

    url_for('static', filename='style.css')

    模板渲染

    render_template() 方法来渲染模板

    情况 1: 模块:

    /application.py
    /templates
        /hello.html
    

    情况 2: 包:

    /application
        /__init__.py
        /templates
            /hello.html
    同样的方式进行引用,只不过包的格式有设定
    1. from flask import render_template
    2. @app.route('/hello/')
    3. @app.route('/hello/<name>')
    4. def hello(name=None):
    5. return render_template('hello.html', name=name)
    @app.route('/hello/')
    @app.route('/hello/<name>')
    def say_hello(name=None):
    name = name
    return render_template('hello.html', name=name)
    接受参数,处理参数然后传递给前端页面,不知道这里可不可以用locals()
    django接受参数用request,处理参数用locals()

    前端html当中的引用和django当中相同
    <title>Hello from Flask</title>{% if name %}<h1>Hello {{ name }}!</h1>{% else %}<h1>Hello World!</h1>{% endif %}
        在模板里,你也可以访问 request 、 session 和 g [1] 对象, 以及get_flashed_messages() 函数。
    可以直接用request和session

    访问请求数据

    与客户端发送给服务器的数据交互至关重要。在 Flask 中由全局的request 对象来提供这些信息
    环境作用域:

    环境局部变量

    Flask 中的某些对象是全局对象,但却不是通常的那种。这些对象实际上是特定环境的局部对象的代理
    一个请求传入,Web 服务器决定生成一个新线程( 或者别的什么东西,只要这个底层的对象可以胜任并发系统,而不仅仅是线程)。 当 Flask 开始它内部的请求处理时,它认定当前线程是活动的环境,并绑定当前的应用和 WSGI 环境到那个环境上(线程)

    单元测试的最简单的解决方案是:用 test_request_context() 环境管理器。结合 with 声明,绑定一个测试请求,这样你才能与之交互。test_request_context() 绑定线程,确定当前活动环境

    另一种可能是:传递整个 WSGI 环境给 request_context() 方法:

    with app.request_context(environ):assert request.method == 'POST'

    app.request_context(environ):
    来进行单元测试

    请求对象

    from flask import request
    1. @app.route('/login', methods=['POST', 'GET'])
    2. def login():
    3. error = None
    4. if request.method == 'POST':
    5. if valid_login(request.form['username'],
    6. request.form['password']):
    7. return log_the_user_in(request.form['username'])
    8. else:
    9. error = 'Invalid username/password'
    10. # the code below is executed if the request method
    11. # was GET or the credentials were invalid
    12. return render_template('login.html', error=error)
    request.form 来访问表单中的数据(POST 或 PUT 请求提交的数据


    你可以通过 args 属性来访问 URL 中提交的参数 ( ?key=value ):

    searchword = request.args.get('q', '')

    文件上传

     HTML 表单中设置enctype="multipart/form-data" 属性
    已上传的文件存储在内存或是文件系统中一个临时的位置
    files属性访问它们。每个上传的文件都会存储在这个字典里
    它还有一个 save() 方法,这个方法允许你把文件保存到服务器的文件系统上。
    from werkzeug import secure_filename
    secure_filename(f.filename))
    http://docs.jinkan.org/docs/flask/patterns/fileuploads.html#uploading-files


    Cookies

    你可以通过 cookies 属性来访问 Cookies,用响应对象的 set_cookie 方法来设置 Cookies。


    重定向和错误

     redirect() 函数把用户重定向到其它地方。
    1. from flask import abort, redirect, url_for
    2. @app.route('/')
    3. def index():
    4. return redirect(url_for('login'))
    5. @app.route('/login')
    6. def login():
    7. abort(401)
    8. this_is_never_executed()



    定制错误页面, 可以使用errorhandler() 装饰器:

    1. from flask import render_template
    2. @app.errorhandler(404)
    3. def page_not_found(error):
    4. return render_template('page_not_found.html'), 404


    关于响应

    视图函数的返回值会被自动转换为一个响应对象。
    如果返回值是一个字符串, 它被转换为该字符串为主体的、状态码为 200 OK``的  MIME 类型是 ``text/html 的响应对象。
    (response, status, headers) 返回的有三个内容

    1. 如果返回的是一个合法的响应对象,它会从视图直接返回。
    2. 如果返回的是一个字符串,响应对象会用字符串数据和默认参数创建。
    3. 如果返回的是一个元组,且元组中的元素可以提供额外的信息。这样的元组必须是(response, status, headers) 的形式,且至少包含一个元素。 status 值会覆盖状态代码, headers 可以是一个列表或字典,作为额外的消息标头值。
    4. 如果上述条件均不满足, Flask 会假设返回值是一个合法的 WSGI 应用程序,并转换为一个请求对象。
    操纵上述步骤结果的响应对象,可以使用 make_response() 函数
    1. @app.errorhandler(404)
    2. def not_found(error):
    3. return render_template('error.html'), 404
    4. 你只需要把返回值表达式传递给 make_response() ,获取结果对象并修改,然后再返回它:
    5. @app.errorhandler(404)
    6. def not_found(error):
    7. resp = make_response(render_template('error.html'), 404)
    8. resp.headers['X-Something'] = 'A value'
    9.     return resp
    escape() 可以在你模板引擎外做转义(如同本例)。
    escape(session['username'])转义成想要的字符串内容
    1. from flask import Flask, session, redirect, url_for, escape, request
    2. app = Flask(__name__)
    3. @app.route('/')
    4. def index():
    5. if 'username' in session:
    6. return 'Logged in as %s' % escape(session['username'])
    7. return 'You are not logged in'
    8. @app.route('/login', methods=['GET', 'POST'])
    9. def login():
    10. if request.method == 'POST':
    11. session['username'] = request.form['username']
    12. return redirect(url_for('index'))
    13. return '''
    14. <form action="" method="post">
    15. <p><input type=text name=username>
    16. <p><input type=submit value=Login>
    17. </form>
    18. '''
    19. @app.route('/logout')
    20. def logout():
    21. # remove the username from the session if it's there
    22. session.pop('username', None)
    23. return redirect(url_for('index'))
    24. # set the secret key. keep this really secret:
    25. app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
    26. 这里提到的 escape() 可以在你模板引擎外做转义(如同本例)。
    27. 如何生成强壮的密钥
    28. 随机的问题在于很难判断什么是真随机。一个密钥应该足够随机。你的操作系统可以基于一个密钥随机生成器来生成漂亮的随机值,这个值可以用来做密钥:
    29. >>> import os
    30. >>> os.urandom(24)
    31. 'xfd{Hxe5<x95xf9xe3x96.5xd1x01O<!xd5xa2xa0x9fR"xa1xa8'





















  • 相关阅读:
    Shipconstructor 2005 破解发布
    活动目录(activate directory)
    上海地铁快成了乞丐天堂
    转载:制造业信息化:计划模拟APS软件驱动敏捷制造
    项目管理的测试版发布
    Introdution RemObject SDK
    怎样在osworkflow中使用BeanShell
    Java class 文件反编译工具 FrontEnd 2.0
    Tribon M3 license keygen
    利用osworkflow实现业务流程
  • 原文地址:https://www.cnblogs.com/wuqingzangyue/p/5749955.html
Copyright © 2020-2023  润新知