1.示例代码

from flask import Flask

app = Flask(__name__,static_url_path='/xx')

@app.route('/index')
def index():
return 'hello world

2.路由加载源码分析

2.1先执行route函数

def route(self, rule, **options):
def decorator(f):
endpoint = options.pop("endpoint", None)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator

2.2 执行add_url_rule函数

def add_url_rule(
self,
rule,
endpoint=None,
view_func=None,
provide_automatic_options=None,
**options
):
if endpoint is None:
endpoint = _endpoint_from_view_func(view_func)
options["endpoint"] = endpoint
methods = options.pop("methods", None) if methods is None:
methods = getattr(view_func, "methods", None) or ("GET",) rule = self.url_rule_class(rule, methods=methods, **options) self.url_map.add(rule)
if view_func is not None:
self.view_functions[endpoint] = view_func
  1. 将 url = /index 和 methods = [GET,POST]endpoint = "index"封装到Rule对象
  2. 将Rule对象添加到 app.url_map中。
  3. 把endpoint和函数的对应关系放到 app.view_functions中。
  4. 当一个请求过来时,先拿路由在app.url_map找对应的别名,再在app.view_functions中找到别名对应的视图函数

最新文章

  1. C# Combobox 设置 value
  2. Firebug在Firefox DevTools 中复活
  3. linux -小记(3) 问题:linux 安装epel扩展源报错
  4. Javascript 中的闭包和引用
  5. 应用程序池“Classic .NET AppPool”将被自动禁用
  6. CSS-Sprite-Generator丨CSS雪碧在线生成器
  7. Java 简单登录MVC
  8. 使用repeater控件显示列表替代treeview
  9. Network 20Q--Q2 How does Google sell ad spaces?
  10. Qgis插件开发之Qgis源码学习
  11. c++在string类源
  12. SSLv3协议、TLSv1.2协议配置不对导致javax.ws.rs.ProcessingException: java.net.SocketException: Connection reset
  13. css实现遮罩层,父div透明,子div不透明
  14. Django学习(3)模板定制
  15. Docker可视化管理工具Shipyard安装与配置
  16. jdbc 报错解决办法
  17. 通过ApplicationContext.getBean(beanName)动态加载数据。
  18. 深入理解JVM(5)——HotSpot垃圾收集器详解
  19. windows下端口映射(端口转发)
  20. update date and keep time

热门文章

  1. OS_进程调度:C++实现
  2. Linux Systemd 详细介绍: Unit、Unit File、Systemctl、Target
  3. android 6.0 权限设置详解
  4. jni不通过线程c回调java的函数 --总结
  5. 在采用K8S之前您必须了解的5件事情
  6. 基于层级表达的高效网络搜索方法 | ICLR 2018
  7. 【服务器】CentOs7系统使用宝塔面板搭建网站,有FTP配置(保姆式教程)
  8. Apache POI 操作Excel(1)--POI简介
  9. 恕我直言你可能真的不会java第10篇-集合元素归约
  10. Spring Security(五) —— 动手实现一个 IP_Login