9 examples of 'url encode python' in Python

Every line of 'url encode python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
234def encode(self, encoding):
235 '''Return the url in an arbitrary encoding'''
236 netloc = self._host
237 if self._port:
238 netloc += (b(':') + bytes(self._port))
239
240 result = urlunparse((
241 self._scheme, netloc, self._path,
242 self._params, self._query, self._fragment
243 ))
244 return result.decode('utf-8').encode(encoding)
242def url_encode(obj, charset='utf-8', encode_keys=False, sort=False, key=None,
243 separator='&'):
244 """URL encode a dict/`MultiDict`. If a value is `None` it will not appear
245 in the result string. Per default only values are encoded into the target
246 charset strings. If `encode_keys` is set to ``True`` unicode keys are
247 supported too.
248
249 If `sort` is set to `True` the items are sorted by `key` or the default
250 sorting algorithm.
251
252 .. versionadded:: 0.5
253 `sort`, `key`, and `separator` were added.
254
255 :param obj: the object to encode into a query string.
256 :param charset: the charset of the query string.
257 :param encode_keys: set to `True` if you have unicode keys.
258 :param sort: set to `True` if you want parameters to be sorted by `key`.
259 :param separator: the separator to be used for the pairs.
260 :param key: an optional function to be used for sorting. For more details
261 check out the :func:`sorted` documentation.
262 """
263 iterable = iter_multi_items(obj)
264 if sort:
265 iterable = list(iterable)
266 iterable.sort(key=key)
267 tmp = []
268 for key, value in iterable:
269 if encode_keys and isinstance(key, unicode):
270 key = key.encode(charset)
271 else:
272 key = str(key)
273 if value is None:
274 continue
275 elif isinstance(value, unicode):
276 value = value.encode(charset)
277 else:
278 value = str(value)
279 tmp.append('%s=%s' % (_quote(key),
280 _quote_plus(value)))
281 return separator.join(tmp)
50def encode(self, long_url):
51 """Encodes a URL to a shortened URL.
52
53 :type long_url: str
54 :rtype: str
55 """
56 key = sha256(long_url.encode()).hexdigest()[:6]
57 self._cache[key] = long_url
58 return self.url + key
81def encode(u):
82 return u.encode('utf8', 'replace')
53def base64url_encode(data):
54 """
55 encodes a url-safe base64 encoded string.
56 """
57
58 s = base64.b64encode(data, "-_")
59 return s.rstrip("=")
54def urlquote_plus(url, safe=''):
55 """
56 A version of Python's urllib.quote_plus() function that can operate on
57 unicode strings. The url is first UTF-8 encoded before quoting. The
58 returned string can safely be used as part of an argument to a subsequent
59 iri_to_uri() call without double-quoting occurring.
60 """
61 return force_text(quote_plus(force_str(url), force_str(safe)))
114def urlquote(s):
115 """
116 Quote (URL-encode) a string with Unicode support. This is a simple wrapper
117 for ``urllib.quote`` (or ``urllib.parse.quote``) that converts the input to
118 UTF-8-encoded bytestring before quoting.
119
120 :param s: URL component to escape
121 :returns: encoded URL component
122 """
123 s = to_bytes(s)
124 return quote(s)
74def encode_obj(obj):
75 '''
76 encode object in one-line string.
77 so we can send it over stdin.
78 '''
79 return base64.b64encode(dill.dumps(obj))
14def encode(data, mime_type='', charset='utf-8', base64=True):
15 """
16 Encode data to DataURL
17 """
18 if isinstance(data, six.text_type):
19 data = data.encode(charset)
20 else:
21 charset = None
22 if base64:
23 data = utils.text(b64encode(data))
24 else:
25 data = utils.text(quote(data))
26
27 result = ['data:', ]
28 if mime_type:
29 result.append(mime_type)
30 if charset:
31 result.append(';charset=')
32 result.append(charset)
33 if base64:
34 result.append(';base64')
35 result.append(',')
36 result.append(data)
37
38 return ''.join(result)

Related snippets