>_ IzzyLab

Hands-on task

This task is step 9 of the course “Encodings and checksums”

URL-encode a string

hard~10 min

The string in /root/query.txt (name=John Doe&city=São Paulo) needs to be safely embedded in a URL. Perform percent-encoding (URL-encoding) of this string — spaces, &, = and non-ASCII characters must become %XX — and write the result to /root/encoded.txt.

What you'll practise:
  • url-encoding
  • percent-encoding
  • python3
  • urllib.parse
  • character encodings

Topic breakdown

A URL can carry only a limited set of ASCII characters, so spaces, separators like & and =, as well as any non-ASCII bytes (for example accented letters such as ã or é) are replaced with sequences of the form %XX — the hexadecimal code of each byte in UTF-8. This is called percent-encoding. The base system has no ready-made command for this, but the Python standard library contains a module for parsing and building URLs with encoding functions. The idea is to read the string, run it through such a function and write the result.

What's checked

  1. the encoded string decodes back to the original
  2. special characters encoded

What it looks like