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 Res
(string, safe='/', encoding=None, errors=None)
| 845 | return res |
| 846 | |
| 847 | def quote(string, safe='/', encoding=None, errors=None): |
| 848 | """quote('abc def') -> 'abc%20def' |
| 849 | |
| 850 | Each part of a URL, e.g. the path info, the query, etc., has a |
| 851 | different set of reserved characters that must be quoted. The |
| 852 | quote function offers a cautious (not minimal) way to quote a |
| 853 | string for most of these parts. |
| 854 | |
| 855 | RFC 3986 Uniform Resource Identifier (URI): Generic Syntax lists |
| 856 | the following (un)reserved characters. |
| 857 | |
| 858 | unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 859 | reserved = gen-delims / sub-delims |
| 860 | gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" |
| 861 | sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
| 862 | / "*" / "+" / "," / ";" / "=" |
| 863 | |
| 864 | Each of the reserved characters is reserved in some component of a URL, |
| 865 | but not necessarily in all of them. |
| 866 | |
| 867 | The quote function %-escapes all characters that are neither in the |
| 868 | unreserved chars ("always safe") nor the additional chars set via the |
| 869 | safe arg. |
| 870 | |
| 871 | The default for the safe arg is '/'. The character is reserved, but in |
| 872 | typical usage the quote function is being called on a path where the |
| 873 | existing slash characters are to be preserved. |
| 874 | |
| 875 | Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings. |
| 876 | Now, "~" is included in the set of unreserved characters. |
| 877 | |
| 878 | string and safe may be either str or bytes objects. encoding and errors |
| 879 | must not be specified if string is a bytes object. |
| 880 | |
| 881 | The optional encoding and errors parameters specify how to deal with |
| 882 | non-ASCII characters, as accepted by the str.encode method. |
| 883 | By default, encoding='utf-8' (characters are encoded with UTF-8), and |
| 884 | errors='strict' (unsupported characters raise a UnicodeEncodeError). |
| 885 | """ |
| 886 | if isinstance(string, str): |
| 887 | if not string: |
| 888 | return string |
| 889 | if encoding is None: |
| 890 | encoding = 'utf-8' |
| 891 | if errors is None: |
| 892 | errors = 'strict' |
| 893 | string = string.encode(encoding, errors) |
| 894 | else: |
| 895 | if encoding is not None: |
| 896 | raise TypeError("quote() doesn't support 'encoding' for bytes") |
| 897 | if errors is not None: |
| 898 | raise TypeError("quote() doesn't support 'errors' for bytes") |
| 899 | return quote_from_bytes(string, safe) |
| 900 | |
| 901 | def quote_plus(string, safe='', encoding=None, errors=None): |
| 902 | """Like quote(), but also replace ' ' with '+', as required for quoting |
no test coverage detected