Python Glossary Русский

Python 3.1+base64function

base64.encodebytes()

legacy interface

Encodes bytes as Base64 in MIME style: a newline after every 76 characters and at the end of the result.

Syntax

base64.encodebytes(s)

A common mistake is using encodebytes instead of b64encode for a token, an HTTP header or a JSON field: the result contains '\n' even for short data, and the header becomes invalid. Newlines are needed only for e-mail and PEM-like files. It replaced the removed encodestring.

Examples

import base64
print(base64.encodebytes(b'hi'), base64.b64encode(b'hi'))  # → b'aGk=\n' b'aGk='
print(base64.encodebytes(b'x' * 60).count(b'\n'))  # → 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy