Handler 作用:
在第5部分讲到,构建一个tornado网站,必须包含一个或者多个handler,这些handler是RequestHandler的子类。每个请求都会被映射到handler中进行处理,处理后再将结果返回给客户端。所以,可以看到hanlder作为客户端请求跟业务服务逻辑间的桥梁,如果拿MVC的模式来类比的话,每个handler就相当于MVC中的Controller。
从上一节的流程可以看出,RequestHandler 类把 _execute 方法暴露给了 application 对象,在这个方法里完成了请求的具体分发和处理。因此,我主要看这一方法(当然还包括init),其它方法在开发应用时自然会用到,还是比较实用的,比如header,cookie,get/post参数的getter/setter方法,都是必须的。
首先是init。负责对象的初始化,在对象被构造时一定会被调用的。
那对象什么时候被调用呢?从上一节可以看到,在.*主机的路径路由列表里,当路径正则匹配了当前请求的路由时,application 就会新建一个 RequestHandler 对象(实际上是子类对象),然后调用 _execute 方法。init 方法接受3个参数 : application, request, **kwargs,分别是application单例,当前请求request 和 kwargs (暂时没被用上。不过可以覆盖initialize方法,里面就有它)。这个kwargs 是静态存储在路由列表里的,它最初是在给 application 设置路由列表时除了路径正则,处理器类之外的第三个对象,是一个字典,一般情况是空的。init 方法也没做什么,就是建立了这个对象和 application, request 的关联就完了。构造器就应该是这样,对吧?
接下来会调用 _execute 方法。
该方法接受三个参数 transforms(相当于 application 的中间件吧,对流程没有影响),*args(用数字做索引时的正则 group),**kwargs(用字符串做键值时的正则 group,与init的类似但却是动态的)。该方法先设定好 transform(因为不管错误与否都算输出,因此中间件都必须到位)。然后,检查 http 方法是否被支持,然后整合路径里的参数,检查 XSRF 攻击。然后 prepare()。这总是在业务逻辑前被执行,在业务逻辑后还有个 finish()。业务逻辑代码被定义在子类的 get/post/put/delete 方法里,具体视详细情况而定。
还有一个 finish 方法,它在业务逻辑代码后被执行。缓冲区里的内容要被输出,连接总得被关闭,资源总得被释放,所以这些善后事宜就交给了 finish。与缓冲区相关的还有一个函数 flush,顾名思义它会调用 transforms 对输出做预处理,然后拼接缓冲区一次性输出 self.request.write(headers + chunk, callback=callback)。
以上,就是 handler 的分析。handler 的读与写,实际上都是依靠 request 对象来完成的,而 request 到底如何呢?且看下回分解
参考:https://www.cnblogs.com/liaofeifight/p/4938987.html
http://www.nowamagic.net/academy/detail/13321023
RequestHanlder作为所有hanlder的父类,我们看看他有哪些方法与接口,子类需要怎样继承?
别人的代码:
import asyncio
import functools
import inspect
import logging
import os
from aiohttp import web
from .errors import APIError
# 工厂模式,生成GET、POST等请求方法的装饰器
def request(path, *, method):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
return func(*args, **kw)
wrapper.__method__ = method
wrapper.__route__ = path
return wrapper
return decorator
get = functools.partial(request, method='GET')
post = functools.partial(request, method='POST')
put = functools.partial(request, method='PUT')
delete = functools.partial(request, method='DELETE')
# RequestHandler目的就是从URL函数中分析其需要接收的参数,从request中获取必要的参数,
# URL函数不一定是一个coroutine,因此我们用RequestHandler()来封装一个URL处理函数。
# 调用URL函数,然后把结果转换为web.Response对象,这样,就完全符合aiohttp框架的要求:
class RequestHandler(object): # 初始化一个请求处理类
def __init__(self, func):
self._func = asyncio.coroutine(func)
async def __call__(self, request): # 任何类,只需要定义一个__call__()方法,就可以直接对实例进行调用
# 获取函数的参数表
required_args = inspect.signature(self._func).parameters
logging.info('required args: %s' % required_args)
# 获取从GET或POST传进来的参数值,如果函数参数表有这参数名就加入
kw = {arg: value for arg, value in request.__data__.items() if arg in required_args}
# 获取match_info的参数值,例如@get('/blog/{id}')之类的参数值
kw.update(request.match_info)
# 如果有request参数的话也加入
if 'request' in required_args:
kw['request'] = request
# 检查参数表中有没参数缺失
for key, arg in required_args.items():
# request参数不能为可变长参数
if key == 'request' and arg.kind in (arg.VAR_POSITIONAL, arg.VAR_KEYWORD):
return web.HTTPBadRequest(text='request parameter cannot be the var argument.')
# 如果参数类型不是变长列表和变长字典,变长参数是可缺省的
if arg.kind not in (arg.VAR_POSITIONAL, arg.VAR_KEYWORD):
# 如果还是没有默认值,而且还没有传值的话就报错
if arg.default == arg.empty and arg.name not in kw:
return web.HTTPBadRequest(text='Missing argument: %s' % arg.name)
logging.info('call with args: %s' % kw)
try:
return await self._func(**kw)
except APIError as e:
return dict(error=e.error, data=e.data, message=e.message)
# 添加一个模块的所有路由
def add_routes(app, module_name):
try:
mod = __import__(module_name, fromlist=['get_submodule'])
except ImportError as e:
raise e
# 遍历mod的方法和属性,主要是找处理方法
# 由于我们定义的处理方法,被@get或@post修饰过,所以方法里会有'__method__'和'__route__'属性
for attr in dir(mod):
# 如果是以'_'开头的,一律pass,我们定义的处理方法不是以'_'开头的
if attr.startswith('_'):
continue
# 获取到非'_'开头的属性或方法
func = getattr(mod, attr)
# 获取有__method___和__route__属性的方法
if callable(func) and hasattr(func, '__method__') and hasattr(func, '__route__'):
args = ', '.join(inspect.signature(func).parameters.keys())
logging.info('add route %s %s => %s(%s)' % (func.__method__, func.__route__, func.__name__, args))
app.router.add_route(func.__method__, func.__route__, RequestHandler(func))
# 添加静态文件夹的路径
def add_static(app):
path = os.path.join(os.path.dirname(__path__[0]), 'static')
app.router.add_static('/static/', path)
logging.info('add static %s => %s' % ('/static/', path))
实践代码:
import abc
import tornado
from concurrent.futures import ThreadPoolExecutor
from tornado import gen
from tornado.concurrent import run_on_executor
from tornado.web import HTTPError
from scpy.logger import get_logger
# 进程数量
NUMBER_OF_EXECUTOR = 4
logger = get_logger(__file__)
class BaseGetRequestHandler(tornado.web.RequestHandler):
"""
http 响应基类
"""
executor = ThreadPoolExecutor(NUMBER_OF_EXECUTOR)
@tornado.web.asynchronous
@gen.coroutine
def get(self, *args, **kwargs):
try:
result = yield self._get(*args, **kwargs)
self.write(result)
except HTTPError as e:
raise e
except Exception as e:
logger.error(e)
raise HTTPError(404, reason="No results")
@run_on_executor
def _get(self, *args, **kwargs):
request = self._get_request_arguments(*args, **kwargs)
res = self._request_service(**request)
return res
@abc.abstractmethod
def _get_request_arguments(self, *args, **kwargs):
raise NotImplementedError('call to abstract method %s._get_request_arguments' % self.__class__)
@abc.abstractmethod
def _request_service(self, **kwargs):
raise NotImplementedError('call to abstract method %s._request_service' % self.__class__)
class HandlerUtils(object):
@staticmethod
def hump_for_output(risk_res):
"""
输出数据转换为驼峰格式
"""
if not isinstance(risk_res, dict):
return TransUtils.obj_2_hump_dict(risk_res)
else:
return TransUtils.dict_2_hump(risk_res)
@staticmethod
def get_risk_model_request(request_handler):
company_name = request_handler.get_argument("companyName", strip=True)
refresh = request_handler.get_argument("refresh", default='0', strip=True)
refresh_network = request_handler.get_argument("refreshNetwork", default='0', strip=True)
if not company_name:
raise HTTPError(400, reason="Query argument companyName cannot be empty string")
return {
'company_name': company_name,
'refresh': refresh,
'refresh_network': refresh_network
}
@staticmethod
def get_request_params(kwargs):
if kwargs:
company_name = kwargs.get('company_name')
refresh = kwargs.get('refresh')
refresh_network = kwargs.get('refresh_network')
else:
raise HTTPError(400, reason="Query argument companyName cannot be empty string")
return company_name, refresh, refresh_network
转换为驼峰表达式
class TransUtils(object):
"""
转换工具类
"""
@staticmethod
def obj_2_hump_dict(obj):
"""
转换对象的所有字段名为驼峰表达式
"""
if hasattr(obj, '__dict__'):
result = {}
for key, value in obj.__dict__.items():
key_group = str(key).split('_')
words = ''
for idx, key_item in enumerate(key_group):
words += key_item if idx == 0 else string.capwords(key_item)
if hasattr(value, '__dict__'):
sub_obj = getattr(obj, key)
result[words] = TransUtils.obj_2_hump_dict(sub_obj)
elif isinstance(value, list):
value_list = []
for i_value in value:
value_list.append(TransUtils.obj_2_hump_dict(i_value))
result[words] = value_list
else:
result[words] = value
return result
else:
return obj
@staticmethod
def dict_2_hump(a_dict):
"""
转换对象的所有字段名为驼峰表达式
"""
for key, value in a_dict.items():
key_group = str(key).split('_')
words = ''
for idx, key_item in enumerate(key_group):
words += key_item if idx == 0 else string.capwords(key_item)
a_dict.pop(key)
if isinstance(value, dict):
TransUtils.dict_2_hump(value)
a_dict[words] = value
elif isinstance(value, list):
value_list = []
for i_value in value:
if isinstance(i_value, dict):
value_list.append(TransUtils.dict_2_hump(i_value))
else:
value_list.append(i_value)
a_dict[words] = value_list
else:
a_dict[words] = value
return a_dict
with 作用:
With语句是什么?
Python’s with statement provides a very convenient way of dealing with the situation where you have to do a setup and teardown to make something happen. A very good example for this is the situation where you want to gain a handler to a file, read data from the file and the close the file handler.
有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个很好的例子是文件处理,你需要获取一个文件句柄,从文件中读取数据,然后关闭文件句柄。
Without the with statement, one would write something along the lines of:
如果不用with语句,代码如下:
file = open("/tmp/foo.txt")
data = file.read()
file.close()
There are two annoying things here. First, you end up forgetting to close the file handler. The second is how to handle exceptions that may occur once the file handler has been obtained. One could write something like this to get around this:
这里有两个问题。一是可能忘记关闭文件句柄;二是文件读取数据发生异常,没有进行任何处理。下面是处理异常的加强版本:
file = open("/tmp/foo.txt")
try:
data = file.read()
finally:
file.close()
While this works well, it is unnecessarily verbose. This is where with is useful. The good thing about with apart from the better syntax is that it is very good handling exceptions. The above code would look like this, when using with:
虽然这段代码运行良好,但是太冗长了。这时候就是with一展身手的时候了。除了有更优雅的语法,with还可以很好的处理上下文环境产生的异常。下面是with版本的代码:
with open("/tmp /foo.txt") as file:
data = file.read()
with如何工作? —— —
while this might look like magic, the way Python handles with is more clever than magic. The basic idea is that the statement after with has to evaluate an object that responds to an __enter__() as well as an __exit__() function.
这看起来充满魔法,但不仅仅是魔法,Python对with的处理还很聪明。基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法。
After the statement that follows with is evaluated, the __enter__() function on the resulting object is called. The value returned by this function is assigned to the variable following as. After every statement in the block is evaluated, the __exit__() function is called.
紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被赋值给as后面的变量。当with后面的代码块全部被执行完之后,将调用前面返回对象的__exit__()方法。
This can be demonstrated with the following example:
下面例子可以具体说明with如何工作:
#!/usr/bin/env python
# with_example01.py
class Sample:
def __enter__(self):
print "In __enter__()"
return "Foo"
def __exit__(self, type, value, trace):
print "In __exit__()"
def get_sample():
return Sample()
with get_sample() as sample:
print "sample:", sample
When executed, this will result in:
行代码,输出如下
bash-3.2$ ./with_example01.py
In __enter__()
sample: Foo
In __exit__()
As you can see, The __enter__() function is executed The value returned by it - in this case “Foo” is assigned to sample The body of the block is executed, thereby printing the value of sample ie. “Foo” The __exit__() function is called. What makes with really powerful is the fact that it can handle exceptions. You would have noticed that the __exit__() function for Sample takes three arguments - val, type and trace. These are useful in exception handling. Let’s see how this works by modifying the above example.
正如你看到的,
__enter__()方法被执行
__enter__()方法返回的值 - 这个例子中是”Foo”,赋值给变量’sample’
执行代码块,打印变量”sample”的值为 “Foo”
__exit__()方法被调用
with真正强大之处是它可以处理异常。可能你已经注意到Sample类的__exit__方法有三个参数- val, type 和 trace。 这些参数在异常处理中相当有用。我们来改一下代码,看看具体如何工作的。
#!/usr/bin/env python
# with_example02.py
class Sample:
def __enter__(self):
return self
def __exit__(self, type, value, trace):
print "type:", type
print "value:", value
print "trace:", trace
def do_something(self):
bar = 1/0
return bar + 10
with Sample() as sample:
sample.do_something()
Notice how in this example, instead of get_sample(), with takes Sample(). It does not matter, as long as the statement that follows with evaluates to an object that has an __enter__() and __exit__() functions. In this case, Sample()’s __enter__() returns the newly created instance of Sample and that is what gets passed to sample.
这个例子中,with后面的get_sample()变成了Sample()。这没有任何关系,只要紧跟with后面的语句所返回的对象有 __enter__()和__exit__()方法即可。此例中,Sample()的__enter__()方法返回新创建的Sample对象,并赋值给变量sample。
When executed:
代码执行后:
bash-3.2$ ./with_example02.py
type:
value: integer division or modulo by zero
trace: at 0x1004a8128>
Traceback (most recent call last):
File "./with_example02.py", line 19, in
sample.do_somet hing()
File "./with_example02.py", line 15, in do_something
bar = 1/0
ZeroDivisionError: integer division or modulo by zero
Essentially, if there are exceptions being thrown from anywhere inside the block, the __exit__() function for the object is called. As you can see, the type, value and the stack trace associated with the exception thrown is passed to this function. In this case, you can see that there was a ZeroDivisionError exception being thrown. People implementing libraries can write code that clean up resources, close files etc. in their __exit__() functions.
实际上,在with后面的代码块抛出任何异常时,__exit__()方法被执行。正如例子所示,异常抛出时,与之关联的type,value和stack trace传给__exit__()方法,因此抛出的ZeroDivisionError异常被打印出来了。开发库时,清理资源,关闭文件等等操作,都可以放在__exit__方法当中。