又是 web.py 页面执行计时,web.py页面执行计时,实现计时的中间件def
又是 web.py 页面执行计时,web.py页面执行计时,实现计时的中间件def
实现计时的中间件
def timespent_processor(handler): starttime = time.time() result = handler() web.ctx.timespent = timespent = time.time() - starttime content_type = dict(web.ctx.headers).get("Content-Type") xml_types = ("text/html", "application/xhtml+xml", "application/xml") if content_type in xml_types: result += "\n<!-- %d ms -->" % (timespent * 1000) return resultapplication.add_processor(timespent_processor)
完整测试
#!/usr/bin/env python#-*- coding:utf-8 -*-import timeimport jsonimport web# ----------# Processors# ----------def timespent_processor(handler): starttime = time.time() result = handler() web.ctx.timespent = timespent = time.time() - starttime content_type = dict(web.ctx.headers).get("Content-Type") xml_types = ("text/html", "application/xhtml+xml", "application/xml") if content_type in xml_types: result += "\n<!-- %d ms -->" % (timespent * 1000) return result# -----------# Application# -----------urls = ("/", "Home", "/json/", "Json")application = web.application(urls, globals())application.add_processor(timespent_processor)# -----------# Controllers# -----------class Home(object): def GET(self): web.header("Content-Type", "text/html") return "<h1>Hello, World</h1>"class Json(object): def GET(self): web.header("Content-Type", "application/json") return {'title': "Hello, World"}# ------# Runner# ------if __name__ == "__main__": application.run()
相关内容
- python code for solving eigenvalue problem by Jacobi’s method,eig
- python queue模块 消息队列,pythonqueue,#!/usr/local
- 使用python3的base64编解码实现字符串的简易加密解密,
- 输出金字塔形数字,输出金字塔形,[Python]代码i=
- python分割文件,,[Python]代码#!
- 卡饭论坛茶舍抢沙发第2版,茶舍沙发,[Python]代码#-
- 用正则表达式过滤掉文件中的指定邮箱地址,正则表达
- 数据字典 查询电话号码或地址,字典电话号码,查找电
- 一段有关于二次函数的代码,一段二次函数代码,1.pyc
- python模仿matlab的tic/toc计时,tictoc,[Python]代码im
评论关闭