又是 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()

评论关闭