博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
An Introduction to Flask 12~15
阅读量:4099 次
发布时间:2019-05-25

本文共 4824 字,大约阅读时间需要 16 分钟。

作者会解释为什么,太需要了

…,we used a variable called request, it fells like the request is a global variable, but in reality, it is not.

If you consider your flask application in production, it will serve multiple clients at the same time, it is important that the request variable is correct for each thread that servering the request, so clearly request cannot be local variable because it will have different values to different threads.
The way flask handles this is to use the contexts.

书中是这样介绍的:

When Flask receives a request from the client, it needs to make a few objects available to the view function that handle it. A good example is the request object, which encapsulates the HTTP request sent by the client.

Flask uses contexts to temporaritly make certain objects globally accessible.

In reality, request cannot be a global variable if you consider that in a multithreaded server the threads are working on different requests from different clients at the same time, so each thread needs to see a different object in request. Contexts enables Flask to make certain variables globally accessible to a thread without interfering with the other threads.

Flask Web开发 中的翻译:

Flask 从客户端收到请求时,要让视图函数能访问一些对象,这样才能处理请求。请求对象就是一个很好的例子,它封装了客户端发送的HTTP 请求。

Flask 使用上下文临时把某些对象变为全局可访问。

事实上,request 不可能是全局变量。试想,在多线程服务器中,多个线程同时处理不同客户端发送的不同请求时,每个线程看到的request 对象必然不同。Falsk 使用上下文让特定的变量在一个线程中全局可访问,与此同时却不会干扰其他线程。


Session

视频中接着讲到了session,跟着视频中的节奏写笔记。

it can be used to store data specific to that client.

session变量中存储着一些用户信息,(比如用户名,从一个网页访问flask应用中的另一个网页,flask也能知道用户信息)

也需要secret_key

builtins.RuntimeError

RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

python代码:

from flask import Flask, render_template, sessionapp = Flask(__name__)@app.route('/')def index():    if 'count' not in session:  #刚开始,session中没有count        session["count"] = 1  # 然后设置count = 1    else:        session["count"] += 1 # 每次刷新页面,count = count + 1    return render_template('index.html',count=session["count"]) #传递count参数(一个数字)到index.html中if __name__ =="__main__":    app.secret_key = "For the session"    app.run(debug=True)

这里写图片描述


Request dispatching

视频中讲的比较概括,使用@app.route('/')这个装饰器时绑定了/index视图函数的映射关系。

然后服务器根据客户端请求的URL找到对应的视图函数,然后返回请求处理结果返回给客户端。

from flask import Flask, render_templateapp = Flask(__name__)@app.route('/')def index():    name = "FengWeilei"    return render_template("index.html",name=name)if __name__ == '__main__':    app.run(debug=True)

app是一个实例化的对象,可以从python中查看app的一些属性

  1. app.url_map记录着路由和视图函数的映射关系。
  2. /指向 index,默认会使用HEADOPTIONGET这些HTTP方法,(注意没有POST
  3. /static/<filename>路由是Flask 添加的特殊路由,用于访问静态文件。
  4. 还有 app.view_functions。(Flask源码中可以看到view_functions是一个字典:self.view_functions = {},注意字典中 key不能重复,即不能有同名的视图函数)

这里写图片描述


Request hooks

视频中的介绍和书中的一样:

Sometimes it is useful to execute code before or after each request is processed. For example, at the start of each request it may be necessary to create a database connection, or authenticate the user making the request. Instead of duplicating the code that does this in every viewfunction, Flask gives you the option to register common functions to be invoked before or after a request is dispatched to a view function.

Request hooks are implemented as decorators. These are four hooks supported by Flask:

  • before_first_request
  • before_request
  • after_request
  • teardown_request

然后视频中的例子:

from flask import Flask, render_template, session, gfrom datetime import datetimeapp = Flask(__name__)@app.before_request  # 用户请求分发给视图函数处理之前,运行这个函数def before_request():  # 写了一个函数,下面两个视图函数都能用。    if 'count' not in session:        session["count"] = 1    else:        session["count"] += 1    g.when = datetime.now().strftime('%H:%M:%S')    # g 一个应用上下文,来自 werkzeug。    # 简答介绍:An object that the application can use for temporary storage during the handling of request.    # This variable is reset with each request.@app.route('/')def index():    return render_template('index.html',count=session["count"], when=g.when)@app.route('/same_index')def same_index():    return render_template('same_index.html',count=session["count"], when=g.when)if __name__ =="__main__":    app.secret_key = "For the session"    app.run(debug=True)

这里写图片描述

这里写图片描述


Responses

处理客户端请求,生成响应,都是在服务器中完成的。

app.run()会创建本地服务器,在看不到的地方会有make_response()方法,将视图函数的返回值变成真正的响应,返回给客户端。

比如在flask代码中直接使用这个make_response()方法:

from flask import Flask, render_template, make_responseapp = Flask(__name__)@app.route('/response')def response():    resp = make_response(render_template('text.txt'), 200 , {
'Content-Type':'text/plain'}) # make_response可以接受三个参数,返回值内容、HTTP状态码、返回值类型的字典 return respif __name__ == "__main__": app.secret_key = "For safety" app.run(debug=True)

这里写图片描述

make_response()或者说视图函数的返回值可以接受3个参数。

视图函数中返回值接收三个参数的例子(专心看这个响应,更多的网页元素以后再看):

这里写图片描述

此外,响应中还有400状态码、json类型的响应、设置cookie等,视频中、书上都有介绍。

你可能感兴趣的文章
9 款你不能错过的 JSON 工具
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
200页!分享珍藏很久的Python学习知识手册(附链接)
查看>>
程序员之神
查看>>
4 岁小女孩给 Linux 内核贡献提交
查看>>
推荐几个私藏很久的技术公众号给大家
查看>>
王垠受邀面试阿里 P9,被 P10 面跪后网上怒发文,惨打 325 的 P10 赵海平回应了!...
查看>>
Python 趣味打怪:147 段简单代码助你从入门到大师
查看>>
卧槽!小姐姐用动画图解 Git 命令,这也太秀了吧?!
查看>>
厉害了!Python 编辑器界的神器 Jupyter ,推出官方可视化 Debug 工具!
查看>>
卧槽!Java 虚拟机竟然还有这些性能调优技巧...
查看>>
听说玩这些游戏能提升编程能力?
查看>>
7 年工作经验,面试官竟然还让我写算法题???
查看>>
被 Zoom 逼疯的歪果仁,造出了视频会议机器人,同事已笑疯丨开源
查看>>
上古语言从入门到精通:COBOL 教程登上 GitHub 热榜
查看>>
再见,Eclipse...
查看>>
超全汇总!B 站上有哪些值得学习的 AI 课程...
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>
神器面世:让你快速在 iOS 设备上安装 Windows、Linux 等操作系统!
查看>>
沙雕程序员在无聊的时候,都搞出了哪些好玩的小玩意...
查看>>