10 examples of 'python encode url' in Python

Every line of 'python encode url' 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
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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)
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)
53def base64url_encode(data):
54 """
55 encodes a url-safe base64 encoded string.
56 """
57
58 s = base64.b64encode(data, "-_")
59 return s.rstrip("=")
81def encode(u):
82 return u.encode('utf8', 'replace')
183def urlquote(val):
184 """
185 Quotes a string for use in a URL.
186
187 >>> urlquote('://?f=1&j=1')
188 '%3A//%3Ff%3D1%26j%3D1'
189 >>> urlquote(None)
190 ''
191 >>> urlquote(u'\u203d')
192 '%E2%80%BD'
193 """
194 if val is None:
195 return ""
196
197 if PY2:
198 if isinstance(val, text_type):
199 val = val.encode("utf-8")
200 else:
201 val = str(val)
202 else:
203 val = str(val).encode("utf-8")
204 return quote(val)
22def base64url_encode(text):
23 padded_b64 = base64.urlsafe_b64encode(text)
24 return padded_b64.replace('=', '') # = is a reserved char
162def quote_and_encode(string):
163 """Encode a bytes string to UTF-8 and then urllib.quote"""
164 return quote(string.encode('UTF_8'))

Related snippets