因为第一部分是关于初始化的部分的,我就没有发布出来~

wsgi.py————第一部分

在分析这个模块之前, 需要了解一下WSGI, 大致了解了之后再继续~

get_current_url()函数

很明显,该函数的作用是获取当前url地址。 代码如下:

def get_current_url(environ, root_only=False, strip_querystring=False,
host_only=False, trusted_hosts=None):
"""
:param environ: the WSGI environment to get the current URL from.
:param root_only: set `True` if you only want the root URL.
:param strip_querystring: set to `True` if you don't want the querystring.
:param host_only: set to `True` if the host URL should be returned.
:param trusted_hosts: a list of trusted hosts, see :func:`host_is_trusted`
for more information.
"""
tmp = [environ['wsgi.url_scheme'], '://', get_host(environ, trusted_hosts)]
cat = tmp.append
if host_only:
return uri_to_iri(''.join(tmp) + '/')
#这里, temp将变成root_only的地址
cat(url_quote(wsgi_get_bytes(environ.get('SCRIPT_NAME', ''))).rstrip('/'))
cat('/')
if not root_only:
cat(url_quote(wsgi_get_bytes(environ.get('PATH_INFO', '')).lstrip(b'/')))
if not strip_querystring:
qs = get_query_string(environ)
if qs:
cat('?' + qs)
return uri_to_iri(''.join(tmp))

注意11~12行, 最开始那个append我也没懂, 网上也找不到, 于是我试了下:

>>> temp = [1,2,3]
>>> temp
[1, 2, 3]
>>> aa = temp.append
>>> aa(2)
>>> temp
[1, 2, 3, 2]

很明显, 当aa = temp.append之后,aa变成了一个函数, aa(1)等效于temp.append(1)

参数host_only的意思是只取host地址,比如http://www.baidu.com/xxx,其host地址就是http://www.baidu.com

函数最后return uri_to_iri, 是把该URI地址转换成IRI(IRI包含unicode字符,URI是ASCII字符编码)

get_query_string()函数

在wsgi.py中, 有很多类似的函数, 用来获得对应的url字段, 这里我拿出一个来分析, 其他的都大同小异

def get_query_string(environ):
qs = wsgi_get_bytes(environ.get('QUERY_STRING', ''))
# QUERY_STRING really should be ascii safe but some browsers
# will send us some unicode stuff (I am looking at you IE).
# In that case we want to urllib quote it badly.
#上面那句我查阅了urllib.parse.quote()方法,意思好像是把部分敏感词汇使用%xx来隐藏, `safe`参数中的部分使用ascii编码,不用隐藏
return try_coerce_native(url_quote(qs, safe=':&%=+$!*\'(),'))

get_query_string(environ) 该函数的作用是把environ变量转换成latin-1编码(程序段中注释说ascii编码较安全, 但很多浏览器发送的是unicode编码的字串, 所以需要统一编码, latin-1向下兼容ascii)

接下来, 在返回值中我们可以看到url_quote函数, 查询源码:

def url_quote(string, charset='utf-8', errors='strict', safe='/:', unsafe=''):
"""URL encode a single string with a given encoding.""" if not isinstance(string, (text_type, bytes, bytearray)):
string = text_type(string)
if isinstance(string, text_type):
string = string.encode(charset, errors)
if isinstance(safe, text_type):
safe = safe.encode(charset, errors)
if isinstance(unsafe, text_type):
unsafe = unsafe.encode(charset, errors)
safe = frozenset(bytearray(safe) + _always_safe) - frozenset(bytearray(unsafe)) #去除unsafe的部分,并转换成bytearray
rv = bytearray()
for char in bytearray(string):
if char in safe:
rv.append(char)
else:
rv.extend(('%%%02X' % char).encode('ascii'))
return to_native(bytes(rv))

从代码中我们可以知道:传入的string和safe和unsafe参数将被转换成类型为string, 编码方式为charset的数据, 其中charset默认为utf-8, 可以自己指定。最后再把string转换成bytearray, 按规则输出

try_coerce_native 在源码中是try_coerce_native=_identity, _identity=lambda x: x,综合起来try_coerce_native(a) = a

在本代码段中,还有个很重要的东西:bytearray()

查阅文档,bytearray(source, encoding, errors) 一共有三个参数,第一个自然是需要转换的内容,第二个是编码方式

为了理解bytearray, 我写了如下的代码:

>>> string = 'aaaa'
>>> temp = bytearray(string)
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
temp = bytearray(string)
TypeError: string argument without an encoding

提示告诉我,需要增加编码方式,于是进行改进:

>>> string = 'aaaa'.encode('utf-8')
>>> temp = bytearray(string)
>>> print(temp)
bytearray(b'aaaa') #注意这个'b'

成功了,然后我又做了如下操作:

>>> for i in temp:
print(i, end=' ') 97 97 97 97

这个和预想的有点不一样啊,为什么不是输出4个a呢?

原来,我们把string编码成utf-8之后,放入了bytearray()中, temp自然也是utf-8编码的,当输出的时候,自然输出的是utf-8的内容了
同时,本例还说说明了bytearray()的对象是可迭代的

这样,我们就能明白url_quote()函数的意义了:

在函数中,先把string和safe和unsafe转成utf-8编码,然后都转成可迭代的bytearray(), 逐位比对string中是否含有safe中的字符,如果有,则不转换,直接输出; 如果没有,则执行rv.extend(('%%%02X' % char).encode('ascii')),从而完成了url地址中query_string部分的转化(专业要求见get_query_string函数中的备注)

('%%%02X' % char): 前两个%%输出一个%, 后面%02X和C语言中一样: 输出2位十进制整数,不足2位的在前面补零

werkzeug源码阅读笔记(二) 上的更多相关文章

  1. werkzeug源码阅读笔记(二) 下

    wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...

  2. Werkzeug源码阅读笔记(三)

    这次主要讲下werkzeug中的Local. 源码在werkzeug/local.py Thread Local 在Python中,状态是保存在对象中.Thread Local是一种特殊的对象,它是对 ...

  3. Werkzeug源码阅读笔记(四)

    今天主要讲一下werkzeug中的routing模块.这个模块是werkzeug中的重点模块,Flask中的路由相关的操作使用的都是这个模块 routing模块的用法 在讲解模块的源码之前,先讲讲这个 ...

  4. Detectron2源码阅读笔记-(二)Registry&build_*方法

    ​ Trainer解析 我们继续Detectron2代码阅读笔记-(一)中的内容. 上图画出了detectron2文件夹中的三个子文件夹(tools,config,engine)之间的关系.那么剩下的 ...

  5. Android源码阅读笔记二 消息处理机制

    消息处理机制: .MessageQueue: 用来描述消息队列2.Looper:用来创建消息队列3.Handler:用来发送消息队列 初始化: .通过Looper.prepare()创建一个Loope ...

  6. Apollo源码阅读笔记(二)

    Apollo源码阅读笔记(二) 前面 分析了apollo配置设置到Spring的environment的过程,此文继续PropertySourcesProcessor.postProcessBeanF ...

  7. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

  8. Three.js源码阅读笔记-5

    Core::Ray 该类用来表示空间中的“射线”,主要用来进行碰撞检测. THREE.Ray = function ( origin, direction ) { this.origin = ( or ...

  9. jdk源码阅读笔记-LinkedHashMap

    Map是Java collection framework 中重要的组成部分,特别是HashMap是在我们在日常的开发的过程中使用的最多的一个集合.但是遗憾的是,存放在HashMap中元素都是无序的, ...

随机推荐

  1. Taum and B'day

    //自己 def main(): t = int(raw_input()) for _ in range(t): units = 0 b, w = map(int, raw_input().strip ...

  2. npm install 本地安装与全局安装

    npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已: npm install grunt # 本地安装 npm install -g gr ...

  3. 图片以BLOB存储在后台数据库中,Android客户端要进行读取显示

    解决方法: 1:在后台以InputStream的方式将图片从数据库中读出: public static InputStream getPicInputStream(){ String id = &qu ...

  4. 必须得是一万小时的 刻意训练(deliberate practice)

    成功素质1:一万小时与格物致知 “格物致知14”的概念,我是从张银奎11老师那里了解到的.它的意思是“推究事物的原理,从而获得知识”,跟我在<透过现象看本质 - 写在观看WWDC 2016 Ke ...

  5. 单线多拨,傻瓜式openwrt单线多拨叠加速率教程

    http://bbs.pceva.com.cn/thread-98362-1-1.html

  6. OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from...

    OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from... 编译的时候碰到一个常见的错误,但是却在一个陌生的地方爆 ...

  7. Spring MVC Controller 单元测试

    简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...

  8. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  9. 在CDHtmlDialog中处理WindowClosing

    要截获window.close(),就得截获CDHtmlDialog的WindowClosing.以下是示例代码: // header DECLARE_EVENTSINK_MAP() void Win ...

  10. poj 3180 The Cow Prom(tarjan+缩点 easy)

    Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...