我试图找到与javascript函数'btoa'完全相同的函数,因为我想将密码编码为base64.看来有很多选项,如下所示:
https://docs.python.org/3.4/library/base64.html
在python中是否有与'btoa'完全相同的东西?
Har*_*son 14
Python的Base64:
import base64
encoded = base64.b64encode('Hello World!')
print encoded
# value of encoded is SGVsbG8gV29ybGQh
Run Code Online (Sandbox Code Playgroud)
Javascript的btoa:
var str = "Hello World!";
var enc = window.btoa(str);
var res = enc;
// value of res is SGVsbG8gV29ybGQh
Run Code Online (Sandbox Code Playgroud)
如您所见,它们都产生相同的结果.
小智 10
我尝试了 python 代码并得到了(使用 python3)
TypeError: a bytes-like object is required, not 'str'
当我添加编码时,它似乎有效
import base64
dataString = 'Hello World!'
dataBytes = dataString.encode("utf-8")
encoded = base64.b64encode(dataBytes)
print(encoded) # res=> b'SGVsbG8gV29ybGQh'
Run Code Online (Sandbox Code Playgroud)