WSGI阅读笔记二

WSGI源码中能够看到,第一个函数是get_current_url,源码以下,仔细分析一下这段代码的做用:html

def get_current_url(environ, root_only=False, strip_querystring=False, host_only=False, trusted_hosts=None):
    
    tmp = [environ['wsgi.url_scheme'], '://', get_host(environ, trusted_hosts)]
    cat = tmp.append
    if host_only:
        return uri_to_iri(''.join(tmp) + '/') 

    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))

get_current_url接受5个参数,其中后四个是默认参数都设置为False或者是None
第一句list中,索引为0的值获取environ字典中keywsgi.url_scheme的值,scheme有两种:http或者是https。索引为2的值是一个地址,其中函数get_hosttrusted_host的做用咱们会在下面的源码中看到。  app

cat是一个函数,至关于list1.append(i)。  
接下来若是host_onlyTrue,那么直接返回字符串,格式如:https://www.xxxxx.xxxx/ 。注意这个uri_to_iri函数位于urls模块中,它的做用是: 将URI转换成用Unicode编码的IRI,IRI的介绍能够查看w3ide

cat(url_quote(wsgi_get_bytes(environ.get('SCRIPT_NAME', ''))).rstrip('/'))中首先get环境字典元素中的script_name,接着wsgi_get_bytes方法将编码方式改为为latin1,在_compat模块中wsgi_get_bytes=operator.methoncaller('encode','latin1')url_quote传入要转换的编码格式的字符串和须要转成的编码格式,将字符串的编码方式改变成给定的形式,默认参数是utf-8。  函数

methodcaller的做用以下:

url_quote源码中有一个bytearray方法是一个内建函数:使用方法编码

>>>d1 = b"12345"
>>>print(type(d1))
>>>d2 = bytearray(d1)
>>>print(type(d2))
>>>for i in d2:
>>>    print d2

由输出结果可知d2是一个"bytearry"类型,且d2是一个可迭代对象。url

下一个条件语句,针对URL中pathquerying进行格式化,并将pathquery加到URL路径中。
相似get_query_string函数,在WSGI中有不少,主要获得的是特定的字符串,get_query_string的理解以下:
里面有一个方法:try_coerce_native其做用是:将字符Unicode字符格式转换成默认的编码格式。  
 
函数最后返回一个通过url_parse处理过的tupleurl_parse返回通过is_test_basedURL或者BytesURL修饰过的字符串。而URLBytesURL两个类从BaseURL中继承。最中仍是返回URL中各个信息段组成的tuplecode

因此get_current_url最后获得一个由URL各信息段组成的tuple

相关文章
相关标签/搜索