Preface
Bloggers recently used Python3 more powerful Django to develop the web, found some URL encoding problem, when the browser submitted the request API, if the URL contains a man, it will be automatically encoded out. The result of the presentation is ==>%xx%xx%xx. If a 3个百 semicolon is an original character, it is UTF8 encoded, if the 2 percent sign is the gb2312 encoding. Here's a demo of coding and decoding code. Coding
From urllib.parse Import quote
Text = quote (text, ' Utf-8 ')
Note: text decodes the string to be encoded
From urllib.parse import unquote
text = unquote (text, ' Utf-8 ')
Source Code
Def unquote (String, encoding= ' Utf-8 ', errors= ' replace '): ""
"replace%xx escapes by their single-character Equivalent. The optional
encoding and errors parameters specify how to decode percent-encoded sequences into
Unicode character S, as accepted by the Bytes.decode () method
.
By default, percent-encoded sequences are decoded with UTF-8, and invalid sequences are from replaced by
a placeholder char Acter.
Unquote (' abc%20def ')-> ' abc def '.
"" " If '% ' not in string:
string.split return
string
if encoding is None:
encoding = ' Utf-8 '
if error S is None:
errors = ' replace '
bits = _asciire.split (string)
res = [Bits[0]]
append = res.append For
I in range (1, Len (bits), 2):
append (Unquote_to_bytes (bits[i)). Decode (encoding, errors))
append ( Bits[i + 1]) return
'. Join (RES)
def quote (string, safe= '/', Encoding=none, Errors=none): "" "Quote (' abc def ')-> ' abc%20def ' each part of a URL
, e.g. the path info, the query, etc., has a different set of reserved characters that must to be quoted.
RFC 2396 Uniform Resource Identifiers (URI): Generic The Syntax lists the following reserved. reserved = ";" | "/" | "?" | ":" | " @" | "&" | "=" |
"+" | "$" |
"," each of these characters are reserved in some component of a URL, but not necessarily in all of them. By default, the quote function is intended to quoting the path section of a URL. Thus, it'll not encode '/'. This character was reserved, but in typical usage the quote function was being called on a path where the existing s
Lash characters are used as reserved characters. String and safe May is either str or bytes objects.
Encoding and errors must the not to specified if string is a bytes object. The optional EncoDing and Errors parameters specify how-deal with Non-ascii, as characters by the accepted method. By default, encoding= ' Utf-8 ' (characters are encoded with UTF-8), and errors= ' strict ' (unsupported characters A
Unicodeencodeerror).
' ' ' If Isinstance (String, str): If not String:return string if encoding ' None: encoding = ' Utf-8 ' If errors is none:errors = ' strict ' string = String.encode (encoding, E rrors) else:if encoding is isn't none:raise typeerror ("quote () doesn ' t support ' encoding ' for byte S ") If errors is not none:raise typeerror (" quote () doesn ' t support ' errors ' to bytes ") return Qu Ote_from_bytes (string, safe)