zoukankan      html  css  js  c++  java
  • WSGI阅读笔记二

    WSGI源码中可以看到,第一个函数是get_current_url,源码如下,仔细分析一下这段代码的作用:

    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的作用我们会在下面的源码中看到。  

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

    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中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中各个信息段组成的tuple

    所以get_current_url最后得到一个由URL各信息段组成的tuple

  • 相关阅读:
    第八章 多线程编程
    Linked List Cycle II
    Swap Nodes in Pairs
    Container With Most Water
    Best Time to Buy and Sell Stock III
    Best Time to Buy and Sell Stock II
    Linked List Cycle
    4Sum
    3Sum
    Integer to Roman
  • 原文地址:https://www.cnblogs.com/hyperionworld/p/5340120.html
Copyright © 2011-2022 走看看