zoukankan      html  css  js  c++  java
  • Python url_escape

    escape.py

    # -*- coding: utf8 -*-
    import sys
    
    if type('') is not type(b''):
        def u(s):
            return s
        bytes_type = bytes
        unicode_type = str
        basestring_type = str
    else:
        def u(s):
            return s.decode('unicode_escape')
        bytes_type = str
        unicode_type = unicode
        basestring_type = basestring
    
    
    try:
        import urllib.parse as urllib_parse  # py3
    except ImportError:
        import urllib as urllib_parse  # py2
    
    
    
    def url_escape(value):
        """Returns a URL-encoded version of the given value."""
        return urllib_parse.quote_plus(utf8(value))
    
    # python 3 changed things around enough that we need two separate
    # implementations of url_unescape.  We also need our own implementation
    # of parse_qs since python 3's version insists on decoding everything.
    if sys.version_info[0] < 3:
        def url_unescape(value, encoding='utf-8'):
            """Decodes the given value from a URL.
    
            The argument may be either a byte or unicode string.
    
            If encoding is None, the result will be a byte string.  Otherwise,
            the result is a unicode string in the specified encoding.
            """
            if encoding is None:
                return urllib_parse.unquote_plus(utf8(value))
            else:
                return unicode_type(urllib_parse.unquote_plus(utf8(value)), encoding)
    
    else:
        def url_unescape(value, encoding='utf-8'):
            """Decodes the given value from a URL.
    
            The argument may be either a byte or unicode string.
    
            If encoding is None, the result will be a byte string.  Otherwise,
            the result is a unicode string in the specified encoding.
            """
            if encoding is None:
                return urllib_parse.unquote_to_bytes(value)
            else:
                return urllib_parse.unquote_plus(to_basestring(value), encoding=encoding)
    
        def parse_qs_bytes(qs, keep_blank_values=False, strict_parsing=False):
            """Parses a query string like urlparse.parse_qs, but returns the
            values as byte strings.
    
            Keys still become type str (interpreted as latin1 in python3!)
            because it's too painful to keep them as byte strings in
            python3 and in practice they're nearly always ascii anyway.
            """
            # This is gross, but python3 doesn't give us another way.
            # Latin1 is the universal donor of character encodings.
            result = _parse_qs(qs, keep_blank_values, strict_parsing,
                               encoding='latin1', errors='strict')
            encoded = {}
            for k, v in result.items():
                encoded[k] = [i.encode('latin1') for i in v]
            return encoded
    
    
    _UTF8_TYPES = (bytes_type, type(None))
    
    
    def utf8(value):
        """Converts a string argument to a byte string.
    
        If the argument is already a byte string or None, it is returned unchanged.
        Otherwise it must be a unicode string and is encoded as utf8.
        """
        if isinstance(value, _UTF8_TYPES):
            return value
        assert isinstance(value, unicode_type)
        return value.encode("utf-8")
    
    _TO_UNICODE_TYPES = (unicode_type, type(None))
    
    
    def to_unicode(value):
        """Converts a string argument to a unicode string.
    
        If the argument is already a unicode string or None, it is returned
        unchanged.  Otherwise it must be a byte string and is decoded as utf8.
        """
        if isinstance(value, _TO_UNICODE_TYPES):
            return value
        assert isinstance(value, bytes_type)
        return value.decode("utf-8")
    
    
    if __name__ == '__main__':
        args = sys.argv[1:]
        if len(args) != 2:
            sys.exit(1)
    
        if args[0] == 'encode':
            print utf8(url_escape(args[1]))
            sys.exit(0)
    
        if args[0] == 'decode':
            print utf8(url_unescape(args[1]))
            sys.exit(0)
    
        
  • 相关阅读:
    UVa 10088 (Pick定理) Trees on My Island
    LA 3295 (计数 容斥原理) Counting Triangles
    LA 5846 (计数) Neon Sign
    java是什么?软帝学院告诉你学Java能做什么?Java有什么特性?
    【软帝学院】一套好的java基础教学视频需要哪些有哪些内容
    推荐五个java基础学习网站,小白必备
    学习java设计模式有用吗?懂这六个原则,编程更轻松
    Java是什么?只需5分钟,了解java必须要知道的知识点
    软帝学院:自学java到底难不难?做好这几步,少走3年弯路
    软帝学院:java开发程序很难吗?学会这十步,5分钟搞定一个程序
  • 原文地址:https://www.cnblogs.com/gibbon/p/Python.html
Copyright © 2011-2022 走看看