码迷,mamicode.com
首页 > 其他好文 > 详细

第二章:程序的基本结构

时间:2016-03-21 15:20:08      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

flask中的helloworld:

 1 from flask import Flask
 2 
 3 app = Flask(__name__)
 4 
 5 @app.route(/)
 6 def index_1():
 7     return hello world
 8 
 9 @app.route(/<name>)
10 def index_2(name):
11     return hello {}.format(name)
12 
13 if __name__ == __main__:
14     app.run(debug=True, port=8888)

flask中的上下文全局变量:

  • current_app:程序上下文: 当前激活程序的程序实例
  • g:程序上下文: 处理请求时用作临时存储的对象,每次请求都会重设这个变量
  • request:请求上下文: 请求对象, 封装了客户端发出的http请求中的内容
  • session:请求上下文: 用户回话, 用于存储请求之间需要“记住”的值的词典

请求调度:flask使用app.route修饰器或者app.add_url_rule()生成映射

请求钩子:在处理请求之前或之后执行某些代码很有用。比如,在请求开始前,需要创建数据库或者认证用户,为了避免使用重复代码,flask提供了注册通用函数的功能。

flask支持以下4中钩子:

  • before_first_request:注册一个函数, 在处理第一个请求之前运行
  • before_request:注册一个函数, 在每次请求之前运行
  • after_request:注册一个函数, 如果没有未处理的异常抛出, 在每次请求之后运行
  • teardow_request:注册一个函数, 即使有未处理的异常抛出, 也会在每次请求之后运行
  • 在请求钩子函数和视图函数之间共享数据一般使用上下文全局变量g
 1 from flask import Flask
 2 
 3 from flask import g
 4 
 5 app = Flask(__name__)
 6 
 7 
 8 # 在debug模式中, request context 不会被pop, 所以teardown_request钩子函数不会执行, 可设置以下参数在debug模式中执行钩子函数
 9 # app.config[‘PRESERVE_CONTEXT_ON_EXCEPTION‘] = False
10 
11 
12 @app.before_first_request
13 def before_first_request_hook():
14     print(before first request)
15 
16 
17 @app.before_request
18 def before_request_hook():
19     g.name = manman
20     print(before request)
21 
22 
23 @app.after_request
24 def after_request_hook(exception):
25     print(after request)
26     return exception
27 
28 
29 @app.teardown_request
30 def teardown_request_hook(exception):
31     print(teardown request)
32     return exception
33 
34 
35 @app.route(/after)
36 def after_request_hook_test():
37     raise Exception
38     return hello
39 
40 
41 @app.route(/before)
42 def before():
43     return hello world + g.name
44 
45 
46 if __name__ == __main__:
47     app.run(debug=True, port=8888)

 

响应:flask默认返回的状态码为200, 如果需要返回其他状态码:

1 @app.route(/)
2 def index():
3     return Bad Request,  400

flask还可以返回Response对象,make_response()函数可接受1个、2个和3个参数,并返回Response对象, 这个对象也可以设置cookie等操作。

有一种特殊的相应类型, 重定向, 状态码为301或302(http状态码相关介绍见:https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81#3xx.E9.87.8D.E5.AE.9A.E5.90.91)。 这种响应没有页面文档。 由于使用比较频繁,flask提供了redirect()辅助函数。

还有一种特殊的相应由abort函数生成,用于错误处理。

 1 from flask import Flask
 2 
 3 from flask import make_response
 4 
 5 from flask import redirect
 6 
 7 from flask import abort
 8 
 9 app = Flask(__name__)
10 
11 
12 @app.route(/400)
13 def http_400():
14     return <h1>Bad Request</h1>, 400
15 
16 
17 @app.route(/302)
18 def http_302():
19     response = make_response(www.google.com, 302)
20     response.location = http://www.google.com
21     return response
22 
23 
24 @app.route(/redirect)
25 def http_redirect_302():
26     return redirect(http://www.google.com)
27 
28 
29 @app.route(/abort)
30 def http_abort():
31     abort(500)  # abort不会把控制权交还给调用它的函数, 而是抛出异常把控制权交给web服务
32 
33 
34 if __name__ == __main__:
35     app.run(debug=True, port=8888)

 

第二章:程序的基本结构

标签:

原文地址:http://www.cnblogs.com/cjh-persist/p/5300317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!