正文
.
b64encode
(
'a'
)
>>>
encoded
'YQ=='
Base64 解码参见快速入门部分介绍。
URL-Safe
•base64.urlsafe_b64encode(s):
•base64.urlsafe_b64decode(s):
Base64 默认会使用+和/, 但是这 2 个字符在 url 中也有特殊含义。使用 urlsafe 可以解决这个问题。 +替换为-, /替换为_。
import
base64
encodes_with_pluses
=
chr
(
251
)
+
chr
(
239
)
encodes_with_slashes
=
chr
(
255
)
*
2
for
original
in
[
encodes_with_pluses
,
encodes_with
_
slashes
]
:
print
'Original
:'
,
repr
(
original
)
print
'Standard encoding:'
,
base64
.
standard_b64encode
(
original
)
print
'URL-safe encoding:'
,
base64
.
urlsafe_b64encode
(
original
)
print
➢执行结果
$
python
base64_urlsafe
.
py
Original
:
'\xfb\xef'
Standard
encoding
: ++
8
=
URL
-
safe
encoding
: --
8
=
Original
:
'\xff\xff'
Standard
encoding
: //
8
=
URL
-
safe
encoding
:
__8
=
其他编码
Base32 包含 26 个大写字母和 2-7 的数字。
• base64.b32encode(s):使用 Base32 编码字符串。s 是要编码的字符串。
• base64.b32decode(s[, casefold[, map01]]):解码 Base32 编码的字符串。s 为要解码的字符串 。
casefold 表示是否允许小写字母。 map01 表示允许 0 表示 0,1 表示 L 。
import
base64
original_string
=
'This is the data, in the clear.'
print
'Original:'
,
original_string
encoded_string
=
base64
.
b32encode
(
original_string
)
print
'Encoded :'
,
encoded_string
decoded_string
=
base64
.
b32decode
(
encoded_string
)
print
'Decoded :'
,
decoded_string
<
code
class
=
"hljs stylus"
>
g
➢执行结果
$
python
base64_base32
.
py
Original
:
This
is
the
data
,
in
the
clear
.
Encoded
: