sanic source code reading 1.1

acb67ea

__init__.py config.py log.py response.py router.py sanic.py server.py

1.router.py

两个属性,一个是 routers字典,用来保存url和对应handler;一个是default,也就是defaulthander

方法两个:get add

2.log.py

返回logger对象 logging.getlogger(__name__)

调用是  log.error("Writing request failed, connection closed")

3.response.py

STATUS_CODES 404 200

class   HTTPResponse

主要是 output方法,调用加property修饰器的body_bytes方法,将

b''.join([
'HTTP/{} {} {}\r\n'.format(version, self.status, STATUS_CODES.get(self.status, 'FAIL')).encode('latin-1'),
'Content-Type: {}\r\n'.format(self.content_type).encode('latin-1'),
'Content-Length: {}\r\n'.format(len(body)).encode('latin-1'),
'Connection: {}\r\n'.format('keep-alive' if keep_alive else 'close').encode('latin-1'),
b'\r\n',
body,
#b'\r\n'
])

构造并返回

提供两个方法json text一个是将json封装成HTTPResponse对象,一个是把普通字符串封装成前面对象

4.server.py

uvloop asyncio ===>async_loop

这里让系统给这几个属性直接分配指定空间,不用在创建新对象的时候占太多内存

猜你喜欢

转载自www.cnblogs.com/billhsu2009/p/8934249.html