1>>> import json
2>>> import base64
3>>> d = {"alg": "ES256"}
4>>> s = json.dumps(d) # Turns your json dict into a str
5>>> print(s)
6{"alg": "ES256"}
7>>> type(s)
8<class 'str'>
9>>> base64.b64encode(s)
10Traceback (most recent call last):
11 File "<stdin>", line 1, in <module>
12 File "/usr/lib/python3.2/base64.py", line 56, in b64encode
13 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
14TypeError: expected bytes, not str
15>>> base64.b64encode(s.encode('utf-8'))
16b'eyJhbGciOiAiRVMyNTYifQ=='
17
1data = '{"hello": "world"}'
2enc = data.encode() # utf-8 by default
3print base64.encodestring(enc)