python base64 decode incorrect padding错误解决方法

yipeiwu_com6年前Python基础

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)

相关文章

python实现dnspod自动更新dns解析的方法

复制代码 代码如下:def ddns():"""用当前ip更新ddns"""headers = {"Content-type": "application/x-www-form-urle...

Windows下安装python2.7及科学计算套装

Windows下安装python2.7及科学计算套装

安装环境及说明 操作系统:64位win7 以下所有安装包已经被我打包至网盘,请移步到 http://www.colafile.com/file/4591550进行下载 因为在64位win...

Python重新引入被覆盖的自带function

幸运的是, 这一问题还是很容易解决的, 我们只需要使用__builtins__: from __builtins__ import int as py_int 这样一来我们又可以...

python发送邮件脚本

本文实例为大家分享了python发送邮件的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # -*- coding: utf-8 -*- impo...

vc6编写python扩展的方法分享

系统环境:VC6 + Python-2.5.4 1、下载Python-2.5.4源码。 2、解压,打开D:\Python-2.5.4\PC\VC6\pcbuild.dsw,编译,D:\P...