本文整理汇总了Python中six.moves.urllib_parse.quote函数的典型用法代码示例。如果您正苦于以下问题:Python quote函数的具体用法?Python quote怎么用?Python quote使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quote函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: inject
def inject(self, span_context, carrier):
if not isinstance(carrier, dict):
raise InvalidCarrierException('carrier not a collection')
# Note: we do not url-encode the trace ID because the ':' separator
# is not a problem for HTTP header values
carrier[self.trace_id_header] = span_context_to_string(
trace_id=span_context.trace_id, span_id=span_context.span_id,
parent_id=span_context.parent_id, flags=span_context.flags)
baggage = span_context.baggage
if baggage:
for key, value in six.iteritems(baggage):
encoded_key = key
if self.url_encoding:
if six.PY2 and isinstance(value, six.text_type):
encoded_value = urllib_parse.quote(value.encode('utf-8'))
else:
encoded_value = urllib_parse.quote(value)
# we assume that self.url_encoding means we are injecting
# into HTTP headers. httplib does not like unicode strings
# so we convert the key to utf-8. The URL-encoded value is
# already a plain string.
if six.PY2 and isinstance(key, six.text_type):
encoded_key = key.encode('utf-8')
else:
if six.PY3 and isinstance(value, six.binary_type):
encoded_value = str(value, 'utf-8')
else:
encoded_value = value
if six.PY3 and isinstance(key, six.binary_type):
encoded_key = str(key, 'utf-8')
# Leave the below print(), you will thank me next time you debug unicode strings
# print('adding baggage', key, '=>', value, 'as', encoded_key, '=>', encoded_value)
header_key = '%s%s' % (self.baggage_prefix, encoded_key)
carrier[header_key] = encoded_value
示例2: reconstruct_url
def reconstruct_url(environ):
secure = environ['wsgi.url_scheme'] == 'https'
if secure:
url = 'wss://'
else:
url = 'ws://'
if environ.get('HTTP_HOST'):
url += environ['HTTP_HOST']
else:
url += environ['SERVER_NAME']
if secure:
if environ['SERVER_PORT'] != '443':
url += ':' + environ['SERVER_PORT']
else:
if environ['SERVER_PORT'] != '80':
url += ':' + environ['SERVER_PORT']
url += quote(environ.get('SCRIPT_NAME', ''))
url += quote(environ.get('PATH_INFO', ''))
if environ.get('QUERY_STRING'):
url += '?' + environ['QUERY_STRING']
return url
示例3: test_users_request
def test_users_request(self):
"""Create a UsersRequest"""
t = self.create_request_object()
self.assertEqual('datasets/Users.odm?studyoid=%s' % quote(t.studyoid),
t.url_path())
t = self.create_request_object(location_oid="1002")
self.assertTrue('locationoid=%s' % quote("1002") in t.url_path())
self.assertTrue('studyoid=%s' % quote(t.studyoid) in t.url_path())
示例4: test_successful_configuration
def test_successful_configuration(self):
"""We can successfully request ProtocolDeviations"""
t = self.create_request_object('Mediflex', 'Dev')
self.assertEqual('datasets/SDTMDataDictionaries.csv?studyid=%s' % quote(t.studyname_environment()),
t.url_path())
t = self.create_request_object('Mediflex', 'Dev', dataset_format="xml")
self.assertEqual('datasets/SDTMDataDictionaries?studyid=%s' % quote(t.studyname_environment()),
t.url_path())
示例5: _get_canonical_query
def _get_canonical_query(self, req):
req_params = urlparse.parse_qsl(urlparse.urlparse(req.url).query,
keep_blank_values=True)
params = []
for key, val in sorted(req_params or []):
params.append('='.join((urlparse.quote(key, safe='~-_.'),
urlparse.quote(val, safe='~-_.'))))
c_params = '&'.join(params)
self.log.debug('canonical query: %s', c_params)
return c_params
示例6: generic
def generic(d):
"""Converts any dict into a URL. The domain (netlog) is always
example.org, and all keys/values of the dict is turned into a
querystring.
>>> generic({'foo':'1', 'bar':'2'})
"http://example.org/?foo=1&bar=2"
"""
querystring = "&".join([quote(k) + "=" + quote(v) for (k, v) in d.items()])
return "http://example.org/?%s" % querystring
示例7: get_outgoing_url
def get_outgoing_url(url):
"""
Bounce a URL off an outgoing URL redirector, such as
outgoing.prod.mozaws.net.
"""
if not settings.REDIRECT_URL:
return url
# django.utils.http._urlparse is a copy of python's urlparse()
# "but uses fixed urlsplit() function".
parsed_url = django_urlparse(url)
url_netloc = parsed_url.netloc
# This prevents a link like javascript://addons.mozilla.org...
# being returned unchanged since the netloc matches the
# safe list see bug 1251023
if parsed_url.scheme not in ['http', 'https']:
return '/'
# No double-escaping, and some domain names are excluded.
if (url_netloc == django_urlparse(settings.REDIRECT_URL).netloc or
url_netloc in settings.REDIRECT_URL_ALLOW_LIST):
return url
url = force_bytes(jinja2.utils.Markup(url).unescape())
sig = hmac.new(force_bytes(settings.REDIRECT_SECRET_KEY),
msg=url, digestmod=hashlib.sha256).hexdigest()
# Let '&=' through so query params aren't escaped. We probably shouldn't
# bother to quote the query part at all.
return '/'.join([settings.REDIRECT_URL.rstrip('/'), sig,
quote(url, safe='/&=')])
示例8: basefile_to_pathfrag
def basefile_to_pathfrag(self, basefile):
"""Given a basefile, returns a string that can safely be used
as a fragment of the path for any representation of that
file. The default implementation recognizes a number of
characters that are unsafe to use in file names and replaces
them with HTTP percent-style encoding.
Example:
>>> d = DocumentStore("/tmp")
>>> realsep = os.sep
>>> os.sep = "/"
>>> d.basefile_to_pathfrag('1998:204') == '1998/%3A204'
True
>>> os.sep = realsep
If you wish to override how document files are stored in
directories, you can override this method, but you should make
sure to also override
:py:meth:`~ferenda.DocumentStore.pathfrag_to_basefile` to
work as the inverse of this method.
:param basefile: The basefile to encode
:type basefile: str
:returns: The encoded path fragment
:rtype: str
"""
safe = '/;@&=+,'
if sys.version_info < (2, 7, 0):
# urllib.quote in python 2.6 cannot handle unicode values
# for the safe parameter. FIXME: We should create a shim
# as ferenda.compat.quote and use that
safe = safe.encode('ascii') # pragma: no cover
return quote(basefile, safe=safe).replace('%', os.sep + '%')
示例9: parse
def parse(src):
""" Returns an element tree create by `LXML <http://lxml.de/>`_.
:param src: A readable object such as a :class:`wex.response.Response`.
"""
if not hasattr(src, 'read'):
return src
etree = _ElementTree()
try:
stream = HTMLStream(src)
# Sometimes we get URLs containing characters that aren't
# acceptable to lxml (e.g. "http:/foo.com/bar?this=array[]").
# When this happens lxml will quote the whole URL.
# We don't want to have to check for this so we just always
# quote it here and then unquote it in the `base_url` function.
quoted_base_url = quote(src.url) if src.url else src.url
while True:
try:
parser = HTMLParser()
fp = replace_invalid_ncr(stream)
etree.parse(fp, parser=parser, base_url=quoted_base_url)
break
except UnicodeDecodeError as exc:
stream.next_encoding()
except IOError as exc:
logger = logging.getLogger(__name__)
logger.warning("IOError parsing %s (%s)", src.url, exc)
root = etree.getroot()
if root is None:
etree._setroot(UNPARSEABLE)
return etree
示例10: setup
def setup(self, api_map=None, **kwargs):
"""
Set up the url with required parameters and method of the request
Params:
`api_map`: Dict with urls and methods for the request
`kwargs`: Optional additional parameters to be attached to the url
"""
if api_map is None:
raise Exception('Resolve must be called with `api_map` argument')
elif api_map.get('url') is None or api_map.get('method') is None:
raise Exception('Resolve must be called with a map with `url` and `method`')
url = api_map['url']
method = api_map['method']
try:
url = url.format(**kwargs)
except KeyError as ke:
raise exceptions.MissingParam('Missing url sdk parameter: %s' % str(ke))
# We percent encode the url so that if any string has spaces,
# commas or any other special character the url will be correctly formed anyway
self.url = urllib_parse.quote(url)
self.method = method
示例11: _extractContentInfo
def _extractContentInfo(self, item):
request = self.request
rename_ids = {}
if "container_rename_button" in request:
for rename_id in request.get('ids', ()):
rename_ids[rename_id] = rename_id
elif "rename_ids" in request:
for rename_id in request.get('rename_ids', ()):
rename_ids[rename_id] = rename_id
retitle_id = request.get('retitle_id')
id, obj = item
info = {}
info['id'] = info['cb_id'] = id
info['object'] = obj
info['url'] = urllib.quote(id.encode('utf-8'))
info['rename'] = rename_ids.get(id)
info['retitle'] = id == retitle_id
zmi_icon = queryMultiAdapter((obj, self.request), name='zmi_icon')
if zmi_icon is None:
info['icon'] = None
else:
info['icon'] = zmi_icon()
dc = IZopeDublinCore(obj, None)
if dc is not None:
info['retitleable'] = canWrite(dc, 'title')
info['plaintitle'] = not info['retitleable']
title = self.safe_getattr(dc, 'title', None)
if title:
info['title'] = title
formatter = self.request.locale.dates.getFormatter(
'dateTime', 'short')
created = self.safe_getattr(dc, 'created', None)
if created is not None:
info['created'] = formatter.format(created)
modified = self.safe_getattr(dc, 'modified', None)
if modified is not None:
info['modified'] = formatter.format(modified)
else:
info['retitleable'] = 0
info['plaintitle'] = 1
sized_adapter = ISized(obj, None)
if sized_adapter is not None:
info['size'] = sized_adapter
return info
示例12: test_build_url_proxied
def test_build_url_proxied(self):
imdb_fr = Imdb(locale="en_FR", cache=False, anonymize=True, proxy_uri="http://someproxywebsite.co.uk?url={0}")
imdb_fr.timestamp = time.mktime(datetime.date.today().timetuple())
url = imdb_fr._build_url(path="/title/maindetails", params={"tconst": "tt1111111"})
expected_url = "http://someproxywebsite.co.uk?url=" + quote("https://app.imdb.com/title/maindetails")
assert url.startswith(expected_url) is True
示例13: _cli_call
def _cli_call(context, name, args):
if context.output_names:
with context.io_manager.with_stdout() as fp:
fp.write('Item Name: ')
fp.write(parse.quote(name.encode('utf8')))
fp.write('\n')
fp.flush()
return CLI()(context.original_main_args + args)
示例14: testQueryEncoding
def testQueryEncoding(self):
method_config = base_api.ApiMethodInfo(request_type_name="MessageWithTime", query_params=["timestamp"])
service = FakeService()
request = MessageWithTime(timestamp=datetime.datetime(2014, 10, 0o7, 12, 53, 13))
http_request = service.PrepareHttpRequest(method_config, request)
url_timestamp = urllib_parse.quote(request.timestamp.isoformat())
self.assertTrue(http_request.url.endswith(url_timestamp))
示例15: test_create_a_sites_metadata_request
def test_create_a_sites_metadata_request(self):
"""Create a SitesMetadataRequest"""
t = self.create_request_object()
self.assertEqual('datasets/Sites.odm?studyoid=%s' % quote(t.studyoid),
t.url_path())
t = self.create_request_object(project_name=None,
environment_name=None)
self.assertEqual('datasets/Sites.odm',
t.url_path())