python的base64.decodestring或者base64.b64decode方法做base64解碼時報錯:
Traceback(most recent calllast):File"/export/www/outofmemory.cn/controllers/user.py",line136,indecryptPassword
encryptPwd=base64.b64decode(encryptPwd)File"/usr/lib/python2.7/base64.py",line76,inb64decoderaiseTypeError(msg)TypeError:Incorrectpadding
解決此問題的方法很簡單建邓,對base64解碼的string補(bǔ)齊等號就可以了抖拦,如下代碼:
def decode_base64(data):
??? """Decode base64, padding being optional.
??? :param data: Base64 data as an ASCII byte string
??? :returns: The decoded byte string.
???? """
??? missing_padding=4-len(data)%4
??? if missing_padding:
? ? ? ? data+=b'='*missing_padding
??? returnbase64.decodestring(data)
??? #returnbase64.b64decode(data)