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 be quoted. The quote function offers a cautious (not minimal) way to quote a string for most of these parts. RFC 3986 Uniform Resource I
(string, safe='/', encoding=None, errors=None)
| 898 | return res |
| 899 | |
| 900 | def quote(string, safe='/', encoding=None, errors=None): |
| 901 | """quote('abc def') -> 'abc%20def' |
| 902 | |
| 903 | Each part of a URL, e.g. the path info, the query, etc., has a |
| 904 | different set of reserved characters that must be quoted. The |
| 905 | quote function offers a cautious (not minimal) way to quote a |
| 906 | string for most of these parts. |
| 907 | |
| 908 | RFC 3986 Uniform Resource Identifier (URI): Generic Syntax lists |
| 909 | the following (un)reserved characters. |
| 910 | |
| 911 | unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 912 | reserved = gen-delims / sub-delims |
| 913 | gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" |
| 914 | sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
| 915 | / "*" / "+" / "," / ";" / "=" |
| 916 | |
| 917 | Each of the reserved characters is reserved in some component of a URL, |
| 918 | but not necessarily in all of them. |
| 919 | |
| 920 | The quote function %-escapes all characters that are neither in the |
| 921 | unreserved chars ("always safe") nor the additional chars set via the |
| 922 | safe arg. |
| 923 | |
| 924 | The default for the safe arg is '/'. The character is reserved, but in |
| 925 | typical usage the quote function is being called on a path where the |
| 926 | existing slash characters are to be preserved. |
| 927 | |
| 928 | Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings. |
| 929 | Now, "~" is included in the set of unreserved characters. |
| 930 | |
| 931 | string and safe may be either str or bytes objects. encoding and errors |
| 932 | must not be specified if string is a bytes object. |
| 933 | |
| 934 | The optional encoding and errors parameters specify how to deal with |
| 935 | non-ASCII characters, as accepted by the str.encode method. |
| 936 | By default, encoding='utf-8' (characters are encoded with UTF-8), and |
| 937 | errors='strict' (unsupported characters raise a UnicodeEncodeError). |
| 938 | """ |
| 939 | if isinstance(string, str): |
| 940 | if not string: |
| 941 | return string |
| 942 | if encoding is None: |
| 943 | encoding = 'utf-8' |
| 944 | if errors is None: |
| 945 | errors = 'strict' |
| 946 | string = string.encode(encoding, errors) |
| 947 | else: |
| 948 | if encoding is not None: |
| 949 | raise TypeError("quote() doesn't support 'encoding' for bytes") |
| 950 | if errors is not None: |
| 951 | raise TypeError("quote() doesn't support 'errors' for bytes") |
| 952 | return quote_from_bytes(string, safe) |
| 953 | |
| 954 | def quote_plus(string, safe='', encoding=None, errors=None): |
| 955 | """Like quote(), but also replace ' ' with '+', as required for quoting |
no test coverage detected