Flask-上下文管理

 

 

 

1. 

 

 

案例1  

class Foo(object):

    def __str__(self):
        return "asdf"

    def __getattr__(self, item):
        return "99"

    def __getitem__(self, item):
        return "87"
    def __add__(self, other):
        return other +1

obj =Foo() print(obj) #asdf print(obj.x) # 99 print(obj.y) # 99 print(obj["x"]) #87 print(obj["y"]) #87 print(obj +8) #9

 

案例二

py1(s11)

DATA ={
    "request":{
        "x1":"xxx1",
        "x2":"xxx2"
    }
}

class LocalProxy(object):

    def get_dict(self):
        return DATA['request']

    def __str__(self):
        return "asdf"

    def __getattr__(self, item):

        data_dict=self.get_dict()
        return data_dict[item]

    def __getitem__(self, item):

        data_dict =self.get_dict()
        return data_dict[item]

    def __add__(self, other):
        return other +1


request =LocalProxy()  #实例化request对象
session
=LocalProxy()

py2

from s11 import request
print(request)
print(request.x1)
print(request.x2)

执行py2 打印结果为

asdf
xxx1
xxx2

 

案例三(案例二的改进版)

 

DATA ={
    "request":{
        "method":"GET",
        "form":{}
    },
    "session":{
        "user":"alex",
        "age":"19",
    }
}

class LocalProxy(object):

    def __init__(self,key):   #此对象增加初始化方法,定义属性 ,将此属性传到实例化方法中.
        self.key =key

    def get_dict(self):
        return DATA[self.key]

    def __str__(self):
        return "asdf"

    def __getattr__(self, item):

        data_dict=self.get_dict()
        return data_dict[item]

    def __getitem__(self, item):

        data_dict =self.get_dict()
        return data_dict[item]

    def __add__(self, other):
        return other +1


request =LocalProxy("request")
session =LocalProxy("session")

 

执行如下文件.

from s11 import request,session

print(request.method)  #GET
print(request.form)    #{}

print(session.user)    #alex
print(session.age)     #19

 

 

 1 from flask import Flask,request,session
 2 
 3 app = Flask(__name__)
 4 
 5 
 6 @app.route('/index')
 7 def index():
 8     # 1. request是LocalProxy对象
 9     # 2. 对象中有method、执行__getattr__
10     print(request.method)
11     # request['method']
12     # request + 1
13 
14     # 1. session是LocalProxy对象
15     # 2. LocalProxy对象的__setitem__
16     session['x'] = 123
17 
18     return "Index"
19 
20 
21 if __name__ == '__main__':
22     app.run()
23     # app.__call__
24     # app.wsgi_app
25 
26 """
27 第一阶段:请求到来
28     将request和Session相关数据封装到ctx=RequestContext对象中。
29     再通过LocalStack将ctx添加到Local中。
30     __storage__ = {
31         1231:{'stack':[ctx(request,session)]}
32     }
33 第二阶段:视图函数中获取request或session
34     方式一:直接找LocalStack获取
35             from flask.globals import _request_ctx_stack
36             print(_request_ctx_stack.top.request.method)
37             
38     方式二:通过代理LocalProxy(小东北)获取
39             from flask import Flask,request
40             print(request.method)
41             
42 """
View Code

 

转载于:https://www.cnblogs.com/mengbin0546/p/10310530.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值