Base64エンコードを行う
base64
モジュールを使う
とても簡単にできる
>>> import base64 >>> s = "hello world!" >>> base64.b64encode(s) >>> base64.b64encode(s.encode("utf-8")) b'aGVsbG8gd29ybGQh'
すごい簡単!!
b64encodeに渡す文字列をUTF-8にエンコードしない場合、以下のようなエラーが出てしまう
>>> base64.b64encode(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/.pyenv/versions/3.6.1/lib/python3.6/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False) TypeError: a bytes-like object is required, not 'str'