werkzeug的Request探究
werkzeug提供了Request类封装请求。位于werkzeug.wrappers模块里。
class Request(
BaseRequest,
AcceptMixin,
ETagRequestMixin,
UserAgentMixin,
AuthorizationMixin,
CommonRequestDescriptorsMixin,
):
"""Full featured request object implementing the following mixins:
- :class:`AcceptMixin` for accept header parsing
- :class:`ETagRequestMixin` for etag and cache control handling
- :class:`UserAgentMixin` for user agent introspection
- :class:`AuthorizationMixin` for http auth handling
- :class:`CommonRequestDescriptorsMixin` for common headers
"""
请求对象有以下规则:
- 请求对象是不可变对象,除非修改内部数据结构。
- 请求对象是线程共享的,不是线程安全的。如果在多线程中使用清加锁。这个规则实际上是针对请求对象作为全局变量而言的,如flask,因此flask实现了request线程隔离。
- 请求对象不能被序列化。
可以看出,这个类是通过继承其他类类实现功能的。基本功能由BaseRequest类提供(意味着如果不需要这些混入类的功能,Request类和BaseRequest类功能一样),其他功能由Mixin类提供。从继承的类可以看出,我们也可以通过继承新的Mixin类来增加功能。默认下继承了5个Mixin类。
- AcceptMixin提供了解析请求头功能。
- ETagResponseMixin提供了etag和cache处理功能。
- UserAgentMixin提供了用户代理内省功能。
- AuthorizationMixin提供了HTTP认证处理。
- CommonRequestDescriptorsMixin提供了各种HTTP请求头字段描述器。
分析
先分析出现较多的两个描述器:@cached_property
和environ_property
。
@cached_property
class cached_property(property):
def __init__(self, func, name=None, doc=None):
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
def __set__(self, obj, value):
obj.__dict__[self.__name__] =