当前位置: 移动技术网 > IT编程>脚本编程>Python > python base64 decode incorrect padding错误解决方法

python base64 decode incorrect padding错误解决方法

2019年03月23日  | 移动技术网IT编程  | 我要评论

欧洲天气预报,网上免费起名,张艺谋的老婆是谁

python的base64.decodestring方法做base64解码时报错:

复制代码 代码如下:

traceback (most recent call last):
  file "/export/www/outofmemory.cn/controllers/user.py", line 136, in decryptpassword
    encryptpwd = base64.b64decode(encryptpwd)
  file "/usr/lib/python2.7/base64.py", line 76, in b64decode
    raise typeerror(msg)
typeerror: incorrect padding

这也算是python的一个坑吧,解决此问题的方法很简单,对base64解码的string补齐等号就可以了,如下代码:
复制代码 代码如下:

        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
            return base64.decodestring(data)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网