Python Web framework decorator to add routing capabilities

First, observe the following code

From the  Python implementation simple HTTP server and MINI WEB frame (WSGI implemented using server and decoupled frame)  of the last version of the code mini_frame:

import time


def index():
    with open("templates/index.html", 'rb') as f:
        content = f.read()
    return content.decode("utf-8")


def login():
    return "----login page----\r\n %s" % time.ctime()


def register():
    return "----register page----\r\n %s" % time.ctime()


def application(env, start_response):
    file_name = env['PATH_INFO']
    if file_name == '/login.py':
        start_response('200 OK', [('Content-Tpye', 'text/html')])
        return login()
    elif file_name == '/register.py':
        start_response('200 OK', [('Content-Tpye', 'text/html')])
        return register()
    elif file_name == '/index.py':
        start_response('200 OK', [('Content-Tpye', 'text/html')])
        return index()
    else:
        start_response('404 NOT FOUND', [])
        return "Not found page..."

We can see in the preceding code implementation, application function to make a judgment on the request by the user to determine if ... else, then decide what to call the function for processing. It is unreasonable, if supported 100 request, which will require some 100 branches.

 

Second, to achieve the mapping between request handler through the dictionary

 

Guess you like

Origin www.cnblogs.com/leokale-zz/p/11984375.html