第三十三节 通过带有参数的装饰器完成路由功能

URL_DICT = dict()

def route(url):
    def set_func(func):
        URL_DICT[url] = func
        def call_func():
            func()
        return call_func
    return set_func

@route('center_p')
def center_p():
    pass

@route('index_p')
def index_p():
    pass

def application(env, start_response):
    '''env是一个空字典,start_response是web服务器里一个方法的引用,函数return的是body'''
    start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
    file_name = env['PATH INFO']

    try:
        return URL_DICT[file_name]()
    except Exception as ret:
        return '产生了异常%s' % str(ret)

猜你喜欢

转载自www.cnblogs.com/kogmaw/p/12602579.html