最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python的Flask路由實(shí)現(xiàn)實(shí)例代碼

 更新時(shí)間:2023年08月09日 10:24:34   作者:zyanwei2018  
這篇文章主要介紹了Python的Flask路由實(shí)現(xiàn)實(shí)例代碼,在啟動(dòng)程序時(shí),python解釋器會(huì)從上到下對代碼進(jìn)行解釋,當(dāng)遇到裝飾器時(shí),會(huì)執(zhí)行,并把函數(shù)對應(yīng)的路由以字典的形式進(jìn)行存儲(chǔ),當(dāng)請求到來時(shí),即可根據(jù)路由查找對應(yīng)要執(zhí)行的函數(shù)方法,需要的朋友可以參考下

路由簡介

  • 路由定義

處理url和函數(shù)之間綁定關(guān)系的程序

  • 路由作用

路由控制訪問的路徑 ,路徑能訪問到什么是由后端來控制的

路由實(shí)現(xiàn)

裝飾器添加路由表實(shí)現(xiàn)路由

  • 采用裝飾器添加路由功能在程序運(yùn)行時(shí),自動(dòng)添加路由表
  • Flask即采用這種模式

函數(shù)裝飾器方式添加路由映射表

# 路由映射表
path_map = {}
def route(url, **kwargs):
    def decorator(f):
        path_map[url] = f
        return f
    return decorator
@route('/')
def hello():
    return 'hello'
@route('/index')
def index():
    return 'index'
print(path_map)
>{'/': <function hello at 0x7fa103cfee50>, '/index': <function index at 0x7fa103cfedc0>}

類裝飾器方式添加路由映射表

# 路由裝飾器
class WsgiApp(object):
    def __init__(self):
        # 定義路由表
        self.routes = {}
    def route(self, path=None):
        def decorator(func):
            self.routes[path] = func
            return func
        return decorator
    def __call__(self, environ, start_response):
        path = environ.get('PATH_INFO')
        if path is None or path not in self.routes.keys():
            status = "400 Not Found"
            header = [('Content-Type', 'text/plain; charset=utf-8')]
            start_response(status, header)
            return [b'Page Not Found']
        else:
            status = "200 OK"
            header = [('Content-Type', 'text/plain; charset=utf-8')]
            start_response(status, header)
            resp = self.routes.get(path)
            if resp is None:
                status = "400 Not Found"
                header = [('Content-Type', 'text/plain; charset=utf-8')]
                start_response(status, header)
                return [b'Page Not Found']
            else:
                return [resp().encode()]
app = WsgiApp()
# 視圖函數(shù)
@app.route('/')
def hello():
    return 'hello'
@app.route('/login')
def login():
    return 'login'
@app.route('/change')
def change():
    return 'update pwd'
if __name__ == '__main__':
    # 啟動(dòng)服務(wù)
    from wsgiref.simple_server import make_server
    server = make_server('127.0.0.1', 8888, app)
    server.serve_forever()

集中管理路由表實(shí)現(xiàn)路由

手動(dòng)添加路由映射表來 集中管理 路由。

  • Django等大型項(xiàng)目一般采用這種方式。
  • 使用時(shí)自己去添加路由映射表和對應(yīng)的視圖函數(shù)
from wsgiref.simple_server import make_server
def hello():
    return 'hello'
def login():
    return 'login'
def change():
    return 'update pwd'
# 路由表
path_dict = {'/': hello,
             '/login': login,
             '/change': change
             }
def app(environ, start_response):
    path = environ.get('PATH_INFO')
    if path is None or path not in path_dict.keys():
        status = "400 Not Found"
        header = [('Content-Type', 'text/plain; charset=utf-8')]
        start_response(status, header)
        return [b'Page Not Found']
    else:
        status = "200 OK"
        header = [('Content-Type', 'text/plain; charset=utf-8')]
        start_response(status, header)
        resp = path_dict.get(path)
        if resp is None:
            status = "400 Not Found"
            header = [('Content-Type', 'text/plain; charset=utf-8')]
            start_response(status, header)
            return [b'Page Not Found']
        else:
            return [resp().encode()]
if __name__ == '__main__':
    server = make_server('127.0.0.1', 8888, app)
    server.serve_forever()

flask路由實(shí)現(xiàn)

在啟動(dòng)程序時(shí),python解釋器會(huì)從上到下對代碼進(jìn)行解釋,當(dāng)遇到裝飾器時(shí),會(huì)執(zhí)行,并把函數(shù)對應(yīng)的路由以字典的形式進(jìn)行存儲(chǔ),當(dāng)請求到來時(shí),即可根據(jù)路由查找對應(yīng)要執(zhí)行的函數(shù)方法
url_map = {
    # '/index': index
}
def route(option):
    def inner(func,*args, **kwargs):
        # return func(*args, **kwargs)
        url_map[option['path']] = func
    return inner
@route({'path': '/index'})
def index(request):
    pass
  •  這里的url_map作為存儲(chǔ)維護(hù)路由函數(shù)對應(yīng)關(guān)系的映射空間
  • 當(dāng)python解釋器從上到下解釋到@route這一行時(shí),會(huì)自動(dòng)執(zhí)行route({‘path’: ‘/index’}),將inner作為返回值,此時(shí)@route({‘path’: ‘/index’})等同于@inner并裝飾index函數(shù)
  • 繼續(xù)執(zhí)行index=inner(index),url_map即存儲(chǔ)’/index’路由對應(yīng)的index函數(shù)

到此這篇關(guān)于Python的Flask路由實(shí)現(xiàn)實(shí)例代碼的文章就介紹到這了,更多相關(guān)Flask路由實(shí)現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

腾冲县| 阜新市| 甘南县| 通海县| 新邵县| 新营市| 承德县| 万全县| 巴林右旗| 新乐市| 汶川县| 梅州市| 永新县| 象山县| 贵溪市| 通州区| 台南县| 常德市| 石阡县| 德格县| 韶关市| 凤城市| 定西市| 内黄县| 同仁县| 周宁县| 皮山县| 涿鹿县| 建德市| 吉木乃县| 伊川县| 西城区| 黔江区| 平乡县| 泰顺县| 长垣县| 江津市| 马龙县| 道孚县| 自贡市| 唐山市|