quopri supports quoted-printable encoding, a MIME transfer format that leaves much ASCII text readable while escaping non-ASCII bytes and equals signs.
How It Works
quopri supports quoted-printable encoding, a MIME transfer format that leaves much ASCII text readable while escaping non-ASCII bytes and equals signs. It is most relevant to email content, not security.
Step-by-Step Example
import quopri
message_bytes = 'Résumé status'.encode('utf-8')
encoded_bytes = quopri.encodestring(message_bytes)
print(encoded_bytes)
print(quopri.decodestring(encoded_bytes).decode('utf-8'))
Understanding the Example
- The expression "'Résumé status'.encode('utf-8')" supplies the value stored as 'message_bytes'. This value is then used or checked by the later statements.
- The expression 'quopri.encodestring(message_bytes)' supplies the value stored as 'encoded_bytes'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
b'R=C3=A9sum=C3=A9 status'
Résumé status
Where This Fits in a Real Program
This transformation changes how bytes are represented for transport. It does not hide the original information or prove authenticity, so avoid using it as a substitute for encryption or a digital signature. When handling text, encode the characters to bytes first and decode restored bytes using the matching character encoding.
Practice: Try It Yourself
Round-trip a UTF-8 message containing accents through quopri.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Confusing quoted-printable with URL encoding or decoding encoded bytes using the wrong character set.
- Do not mistake successful decoding or parsing for trust in the contents.
Key Takeaways
- quopri supports quoted-printable encoding, a MIME transfer format that leaves much ASCII text readable while escaping non-ASCII bytes and equals signs.
- quopri supports quoted-printable encoding, a MIME transfer format that leaves much ASCII text readable while escaping non-ASCII bytes and equals signs.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.