def quote_plus(s, safe=''):
"""Quote the query fragment of a URL; replacing ' ' with '+'"""
if ' ' in s:
s = quote(s, safe + ' ')
return s.replace(' ', '+')
return quote(s, safe)
python类quote()的实例源码
def format_endpoint(self, resource_id):
"""
This method concatenates the resource's endpoint with a specified
identifier.
Example: endpoint/<pk>/
"""
if isinstance(resource_id, unicode):
resource_id = resource_id.encode("utf-8")
return urljoin(self.endpoint, quote(
str(resource_id))) + TRAILING_SLASH
def test_get_auth_from_url(self):
"""Ensures that username and password in well-encoded URI as per
RFC 3986 are correclty extracted."""
from requests.utils import get_auth_from_url
from requests.compat import quote
percent_encoding_test_chars = "%!*'();:@&=+$,/?#[] "
url_address = "request.com/url.html#test"
url = "http://" + quote(
percent_encoding_test_chars, '') + ':' + quote(
percent_encoding_test_chars, '') + '@' + url_address
(username, password) = get_auth_from_url(url)
assert username == percent_encoding_test_chars
assert password == percent_encoding_test_chars