Does python have the javascript equivalent of 'btoa'

I am trying to find the exact equivalent of the javascript 'btoa' function as I want to encode the password as base64. There seem to be many options listed here:

https://docs.python.org/3.4/library/base64.html

Is there an exact equivalent of 'btoa' in python?

+8
source share
2 answers

Python Base64 :

import base64 encoded = base64.b64encode('Hello World!') print encoded # value of encoded is SGVsbG8gV29ybGQh 

Javascript btoa :

 var str = "Hello World!"; var enc = window.btoa(str); var res = enc; // value of res is SGVsbG8gV29ybGQh 

As you can see, both of them give the same result.

+12
source

I tried python code and got (with python3) TypeError TypeError: a bytes-like object is required, not 'str'

When I added the coding it seems to work

 import base64 dataString = 'Hello World!' dataBytes = dataString.encode("utf-8") encoded = base64.b64encode(dataBytes) print(encoded) # res=> b'SGVsbG8gV29ybGQh' 
0
source

All Articles