python利用装饰器记录每个http请求

设置装饰器

from functools import wraps
from datetime import datetime """
@function 用于记录每个http请求
""" def record_http_request(func):
@wraps(func)
def record(self, *args, **kwargs):
request_time = str(datetime.now())
response = func(self, *args, **kwargs)
http_request = dict(
request_time=request_time,
expend_time=self.request.request_time(),
response_time=str(datetime.now()),
request_ip=self.request.remote_ip,
method=self.request.method,
url=self.request.uri,
request_params=self.request.arguments,
response_code=self.get_status(),
response_text=self.response_value,
)
print(http_request)
return response return record

然后在每个请求函数上面加上这个装饰器

from abc import ABC
from tornado.web import RequestHandler
from tornado.gen import coroutine
from handler.record_infomation import record_http_request class Index(RequestHandler, ABC):
@coroutine
@record_http_request
def get(self, *args, **kwargs):
self.response_value = {"status": 200, "message": "dddd"}
self.write(self.response_value)

这个时候我们那就可以看到装饰器中的print值为

{'request_time': '2019-04-06 19:16:54.951968', 'expend_time': 0.000576019287109375, 'response_time': '2019-04-06 19:16:54.952035', 'request_ip': '192.168.0.102', 'method': 'GET', 'url': '/', 'request_params': {}, 'response_code': 200, 'response_text': {'status': 200, 'message': 'dddd'}}

值已经拿到了,是存库还是记录到日志文件就随便了

tornado利用装饰器记录每个http请求的更多相关文章

  1. flask中的endpoint、自定义转化器、与djnago中session区别、利用装饰器实现登录认证

    flask路由中的endpoint 与自定义转化器 ''' endpoint主要用于 反向解析, 例如:login函数中配的路由是/login,其中endpoint='lg' 则在其他函数,可以用 u ...

  2. cbv装饰器 中间件 跨站请求伪造

    给cbv下面的函数加装饰器 写一个验证用户登录的程序 前端页面 # 写一个装饰器验证session def login_auth(func): def inner(request,*args,**kw ...

  3. tornado登陆装饰器

    tornado作为鼎鼎大名的web异步框架,用来作为高性能服务器以及web框架都是首选.自从python3.4加入了asyncio原生协程后,tornado的最新版本也开始使用了原生的协程.定义协程函 ...

  4. Decorator - 利用装饰器武装前端代码

    历史 以前做后端时,接触过一点Spring,也是第一次了解DI.IOC等概念,面向切面编程,对于面向对象编程还不怎么熟练的情况下,整个人慌的一批,它的日志记录.数据库配置等都非常方便,不回侵入到业务代 ...

  5. 【Flask】 python学习第一章 - 4.0 钩子函数和装饰器路由实现 session-cookie 请求上下文

    钩子函数和装饰器路由实现 before_request 每次请求都会触发 before_first_requrest  第一次请求前触发 after_request  请求后触发 并返回参数 tear ...

  6. Django之CBV装饰器,跨站请求伪造,auth认证

    CBV加装饰器 基于session实现登录 def login(request): if request.method == 'POST': username = request.POST.get(' ...

  7. python cookbook第三版学习笔记二十一:利用装饰器强制函数上的类型检查

    在演示实际代码前,先说明我们的目标:能对函数参数类型进行断言,类似下面这样: @typeassert(int, int) ... def add(x, y): ...     return x + y ...

  8. JavaBIO利用装饰器模式来组织和扩展接口

    Stream接口,它直接负责字节流的传输. Reader/Writer接口,它本身不能读直接读写数据,而是以Stream接口为内部核心,在外围装饰增强,负责字符流的读写.字符和字节的转换过程必须指定字 ...

  9. 【Python】装饰器实现日志记录

    好的日志对一个软件的重要性是显而易见的.如果函数的入口都要写一行代码来记录日志,这种方式实在是太低效了,但一直没有找到更好的方法.后来用python写一些软件,了解到python的装饰器功能时,突然人 ...

随机推荐

  1. proxychains4配置使用

    一丶安装 sudo apt-get install proxychains4 二丶修改配置文件 sudo vim /etc/proxychains.conf 在文本最后加上你的代理服务器地址,如果有用 ...

  2. Vue axios异步获取后台数据alert提示undefined

    记录一个小问题,关于分页查询套餐 前台通过axios异步请求获取后台数据alert弹出数据提示undefined 下面有三个bean PageResult /** * 分页结果封装对象 */ publ ...

  3. mysql 常用 sql 语句 - 快速查询

    Mysql 常用 sql 语句 - 快速查询 1.mysql 基础 1.1 mysql 交互         1.1.1 mysql 连接             mysql.exe -hPup    ...

  4. TCP 通信时序及状态变迁

    TCP 通信时序及状态变迁 参考链接: https://www.cnblogs.com/boxker/p/11214886.html https://blog.csdn.net/miss_ruoche ...

  5. matplotlib---画等高线

    contour - 绘制等高线 mp.contour(x, y, z, 等高线条数,colors=颜色, linewidth=线宽)#等高线绘制 contourf - 填充等高线 mp.contour ...

  6. 【AICC】2019训练营笔记

    1.AI 人工的方法在机器上实现智能:机器学习.计算机视觉.规划决策.自然语言处理.认知推理.高效搜索 2.三大学派 符号主义 连接主义:CNN 行为主义 3.两条路线 结构模仿 功能模仿 4.AI芯 ...

  7. Git 游离态的一次问题解决

    jie@mozq MINGW64 /d/0xcEdu/xcEduService01 ((20ce6a5...)) $ git branch -v * (HEAD detached at 20ce6a5 ...

  8. 【oracle】ORA-12638

    背景:换电脑时将旧电脑的ORACLE的登陆信息转到新电脑.其中有三个文件:listener.ora   sqlnet.ora  tnsnames.ora 解决办法:删了sqlnet.ora 原因:

  9. python paramiko与linux的连接

    两种使用paramiko连接到linux服务器的代码 方式一: 1 ssh = paramiko.SSHClient() 2 ssh.set_missing_host_key_policy(param ...

  10. 基于Linux(中标麒麟)上QT的环境搭建

    最近由于公司需要,需要在中标麒麟上进行QT的二次开发,但是网上的资料很少,就算是有也基本都是其他的版本的Linux上的搭建.中标麒麟本身的资料也很好,而且还只能试用60天. 下面就介绍下我对此环境的搭 ...