学习闲暇时间,把写代码过程中比较常用的内容收藏起来,下面的代码是关于python中base64加密解密方法的代码,应该是对码农有用。
C:Python27>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> str = 'haha'
>>> base64.b64encode(str)
'aGFoYQ=='
>>> base64.b64decode('aGFoYQ==')
'haha'
三其他的方法,这个比较重要
base64.b64encode(s[, altchars])
base64.b64decode(s[, altchars])
altchars为可选的参数,用来替换+和/的一个两个长度的字符串。
base64.urlsafe_b64encode(s)
base64.urlsafe_b64decode(s)
此方法中用-代替了+,用_代替了/,这样可以保证编码后的字符串放在url里可以正常访问
base64.b32encode(s)
base64.b32decode(s[, casefold[, map01]])
base64.b16encode(s)
base64.b16decode(s[, casefold])