Python3 URL encoding and decoding

Source: Internet
Author: User
Tags reserved
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.