Python urlencode与urldecode的使用
需求:有时候我们GET方法参数的特殊符号,如果没有编码就会被截取,这里时候,就需要客户端需要urlencode,服务端:urldecode来进行处理
注意:此代码来源Tornado源码
#!/usr/bin/env python # -*- coding: utf-8 -*- import typing import urllib.parse from typing import Union, Optional _UTF8_TYPES = (bytes, type(None)) unicode_type = str @typing.overload def utf8(value: bytes) -> bytes: pass @typing.overload # noqa: F811 def utf8(value: str) -> bytes: pass @typing.overload # noqa: F811 def utf8(value: None) -> None: pass def utf8(value: Union[None, str, bytes], code_type='utf-8') -> Optional[bytes]: # noqa: F811 """ utf-8编码""" if isinstance(value, _UTF8_TYPES): return value if not isinstance(value, unicode_type): raise TypeError("Expected bytes, unicode, or None; got %r" % type(value)) return value.encode(code_type) @typing.overload def to_unicode(value: str) -> str: pass @typing.overload # noqa: F811 def to_unicode(value: bytes) -> str: pass @typing.overload # noqa: F811 def to_unicode(value: None) -> None: pass _TO_UNICODE_TYPES = (unicode_type, type(None)) def to_unicode(value: Union[None, str, bytes], code_type='utf-8') -> Optional[str]: # noqa: F811 """字节类型转为字符串""" if isinstance(value, _TO_UNICODE_TYPES): return value if not isinstance(value, bytes): raise TypeError("Expected bytes, unicode, or None; got %r" % type(value)) return value.decode(code_type) def url_escape(value: Union[str, bytes], plus: bool = True) -> str: """ url编码的功能 plus=True 表示空格用+替换 plus=False 表示空格用%20替换 """ quote = urllib.parse.quote_plus if plus else urllib.parse.quote return quote(utf8(value)) def url_unescape( # noqa: F811 value: Union[str, bytes], encoding: Optional[str] = "utf-8", plus: bool = True ) -> Union[str, bytes]: """ url解码的功能 plus=True 表示空格用+替换 plus=False 表示空格用%20替换 """ if encoding is None: if plus: # 将字节类型转为字符串类型,并且替换+号为空格 value = to_unicode(value).replace("+", " ") return urllib.parse.unquote_to_bytes(value) else: unquote = urllib.parse.unquote_plus if plus else urllib.parse.unquote return unquote(to_unicode(value), encoding=encoding) if __name__ == '__main__': url_escape_ret = url_escape("username=test&password=admin&content=中国 人", plus=False) url_unescape_ret = url_unescape(url_escape_ret, plus=False) print('编码', url_escape_ret, type(url_escape_ret)) print('解码', url_unescape_ret, type(url_unescape_ret))
运行结果
编码 username%3Dtest%26password%3Dadmin%26content%3D%E4%B8%AD%E5%9B%BD%20%20%E4%BA%BA <class 'str'> 解码 username=test&password=admin&content=中国 人 <class 'str'>