URL篇

在分析路由匹配过程之前,我们先来看看 flask 中,构建这个路由规则的两种方法:

  1. 通过 @app.route() decorator

  2. 通过 app.add_url_rule,这个方法的签名为 add_url_rule(self, rule, endpoint=None, view_func=None, **options),参数的含义如下:

    • rule: url 规则字符串,可以是静态的 /path,也可以包含 /

    • endpoint:要注册规则的 endpoint,默认是 view_func 的名字

    • view_func:对应 url 的处理函数,也被称为视图函数

这两种方法是等价的,也就是说:

@app.route('/')
def hello():
return "hello, world!"

也可以写成

def hello():
return "hello, world!" app.add_url_rule('/', 'hello', hello)

其实,还有一种方法来构建路由规则——直接操作 app.url_map 这个数据结构。不过这种方法并不是很常用,因此就不展开了

静态路由

@app.route('/')
def hello_world():
# 变量可以通过赋值传到前端,前端可以通过Jinja语法{{}}渲染
return render_template('t1.html', name='t1', age=16) @app.route('/services')
def services():
return 'Services'
@app.route('/about')
def about():
return 'About' # 相对projects解释类似于文件夹解释形式,指向某一个文件夹下的某个文件
@app.route('/projects/')
@app.route('/projects_our') # 可以定义多个URL到同一个视图函数上,Flask支持
def projects():
return 'Projects' @app.route('/login',methods=["GET","POST"])
def login():
return render_template('login.html', req_method=request.method)

动态路由

# 动态路由
@app.route('/user/<username>')
def user(username):
print username
return username # 路由转换器:指定参数类型
# flask提供3种:int(整形)|float(浮点型)|path(路径,并支持一个/)
@app.route('/user/<int:user_id>')
def user(user_id):
print user_id
return 'User_id:%s'%user_id

自定义路由规则

# flask不提供正则表达的形式的URL匹配
# 可通过定义完成
# 1、from werkzeug.routing import BaseConverter
# 2、自定义类
#转换器
class RegexConverter(BaseConverter):
def __init__(self,url_map,*items):
super(RegexConverter,self).__init__(self)
# print items # (u'[a-z]{3}[A-Z]{3}',)
# print url_map # URL 的一个MAP对象,类似路由表
self.regex = items[0] # 3、要将定义的类注册到APP的url_map中,定义名称
# app.url_map.converters['regex'] = RegexConverter # 4、使用
@app.route('/user/<regex("[a-z]{3}[A-Z]{3}"):username>')
def user(username):
print username
return 'Username:%s' % username

浅析源码

注册路由规则的时候,flask 内部做了哪些东西呢?我们来看看 route 方法:

def route(self, rule, **options):
"""A decorator that is used to register a view function for a
given URL rule. This does the same thing as :meth:`add_url_rule`
but is intended for decorator usage.
""" def decorator(f):
endpoint = options.pop('endpoint', None)
self.add_url_rule(rule, endpoint, f, **options)
return f return decorator

route

route 方法内部也是调用 add_url_rule,只不过在外面包了一层装饰器的逻辑,这也验证了上面两种方法等价的说法

def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
"""Connects a URL rule. Works exactly like the :meth:`route`
decorator. If a view_func is provided it will be registered with the
endpoint.
"""
if endpoint is None:
endpoint = _endpoint_from_view_func(view_func)
options['endpoint'] = endpoint
methods = options.pop('methods', None) # if the methods are not given and the view_func object knows its
# methods we can use that instead. If neither exists, we go with
# a tuple of only ``GET`` as default.
if methods is None:
methods = getattr(view_func, 'methods', None) or ('GET',)
if isinstance(methods, string_types):
raise TypeError('Allowed methods have to be iterables of strings, '
'for example: @app.route(..., methods=["POST"])')
methods = set(item.upper() for item in methods) # Methods that should always be added
required_methods = set(getattr(view_func, 'required_methods', ())) # starting with Flask 0.8 the view_func object can disable and
# force-enable the automatic options handling.
provide_automatic_options = getattr(view_func,
'provide_automatic_options', None) if provide_automatic_options is None:
if 'OPTIONS' not in methods:
provide_automatic_options = True
required_methods.add('OPTIONS')
else:
provide_automatic_options = False # Add the required methods now.
methods |= required_methods rule = self.url_rule_class(rule, methods=methods, **options)
rule.provide_automatic_options = provide_automatic_options self.url_map.add(rule)
if view_func is not None:
old_func = self.view_functions.get(endpoint)
if old_func is not None and old_func != view_func:
raise AssertionError('View function mapping is overwriting an '
'existing endpoint function: %s' % endpoint)
self.view_functions[endpoint] = view_func

add_url_rule

上面这段代码省略了处理 endpoint 和构建 methods 的部分逻辑,可以看到它主要做的事情就是更新 self.url_mapself.view_functions 两个变量。找到变量的定义,发现 url_mapwerkzeug.routeing:Map 类的对象,rulewerkzeug.routing:Rule 类的对象,view_functions 就是一个字典。这和我们之前预想的并不一样,这里增加了 RuleMap 的封装,还把 urlview_func 保存到了不同的地方。

需要注意的是:每个视图函数的 endpoint 必须是不同的,否则会报 AssertionError

未完待续。。。

flask_之URL的更多相关文章

  1. HTML URL地址解析

    通过JavaScript的location对象,可获取URL中的协议.主机名.端口.锚点.查询参数等信息. 示例 URL:http://www.akmsg.com/WebDemo/URLParsing ...

  2. URL安全的Base64编码

    Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可 ...

  3. Android业务组件化之URL Scheme使用

    前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...

  4. ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系

    ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...

  5. Node.js:path、url、querystring模块

    Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...

  6. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  7. MVC通过路由实现URL重写

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...

  8. 【转】java通用URL接口地址调用方式GET和POST方式

    java通用URL接口地址调用方式GET和POST方式,包括建立请求和设置请求头部信息等等......... import java.io.ByteArrayOutputStream; import ...

  9. linux字符串url编码与解码

    编码的两种方式 echo '手机' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' echo '手机' |tr -d '\n' |od -An -tx ...

随机推荐

  1. PAT 天梯赛 L2-008. 最长对称子串 【字符串】

    题目链接 https://www.patest.cn/contests/gplt/L2-008 思路 有两种思路 第一种 遍历每一个字符 然后对于每一个 字符 同时 往左 和 往右 遍历 只要 此时 ...

  2. 7-2 jmu-python-猜数游戏(10 point(s)) 【python】

    7-2 jmu-python-猜数游戏(10 point(s)) 用户从键盘输入两个整数,第一个数是要猜测的数n(<10),第二个数作为随机种子,随机生成一个1~10的整数,如果该数不等于n,则 ...

  3. web安全字体

    webfont解剖 Unicode字体可以包含数以千计字形 有四个字体格式: WOFF2, WOFF, EOT, TTF 一些字体格式需要使用GZIP压缩 一个web字体是字形的集合,且每个字形是一个 ...

  4. Ubuntu 16.04上安装SkyEye及测试

    说明一下,在Ubuntu 16.04上安装SkyEye方法不是原创,是来自互联网,仅供学习参考. 一.检查支持软件包 gcc,make,vim(optional),ssh,subversionbinu ...

  5. 为什么在启动linux后进入终端提示sh-3.2#

    这是Linux系统环境变量设置问题,用户登陆后确保是root权限,可以用如下这两条命令解决:-bash-3.2# cp /etc/skel/.{bash_profile,bashrc} ~-bash- ...

  6. python 基础之第三天

    例子1:8位数的随机密码 #!/usr/bin/python # coding:utf-8 import string import random pwd = '' for i in range(8) ...

  7. javascript之闭包,递归,深拷贝

    闭包 理解:a函数执行后return出b函数且b函数可以访问a函数的数据 好处:子函数存储在复函数内部,子函数执行完不会被自动销毁 坏处:占用内存比较大 ex: function bibao(){ v ...

  8. SecureCRT远程连接虚拟机CentOS的三种方式

    当在VMware虚拟机中将CentOS安装成功后,会在win7系统中模拟出两个虚拟网卡:VMnet1和VMnet8,我们来查看一下,点击“控制面板—>查看网络状态和任务—>更改适配器设置” ...

  9. 火狐浏览器安装VULTR笔记

    1.购买一台vultr服务器, 支持支付宝扫码支付,直接美刀转人民币实时结算:优先选日本的,然后美国的; 购买服务器步骤: Server Location: Tokyo Japan Server Ty ...

  10. 增加,删除GMS包

    1. device/hiteq/vtab_1050_standard/httek.mk BUILD_GMS:=yes GMS_VARIANT:=mini 2. rm out/target/produc ...