File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ '练习内建模块之base64'
5
+
6
+ __author__ = 'sergiojune'
7
+ import base64
8
+
9
+ # 进行base64编码,转为字符串
10
+ b = b'binary\x00 strg='
11
+ bs = base64 .b64encode (b )
12
+ print (bs )
13
+ # 解码
14
+ b = base64 .b64decode (bs )
15
+ print (b )
16
+
17
+
18
+ # 对于网页的安全编码
19
+ s = b'i\xb7 \x1d \xfb \xef \xff '
20
+ bs = base64 .b64encode (s )
21
+ print (bs )
22
+ bs = base64 .urlsafe_b64encode (s )
23
+ print (bs )
24
+
25
+
26
+ # 作业:请写一个能处理去掉=的base64解码函数
27
+ def safe_base64_decode (s ):
28
+ while len (s ) % 4 != 0 :
29
+ s += b'='
30
+ bs = base64 .b64decode (s )
31
+ return bs
32
+
33
+
34
+ # 测试:
35
+ assert b'abcd' == safe_base64_decode (b'YWJjZA==' ), safe_base64_decode ('YWJjZA==' )
36
+ assert b'abcd' == safe_base64_decode (b'YWJjZA' ), safe_base64_decode ('YWJjZA' )
37
+ print ('ok' )
You can’t perform that action at this time.
0 commit comments