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脚本成为Windows中运行的exe文件

将程序转换为exe文件 我们先来介绍如何使用工具Pyinstaller 安装Pyinstaller 我们用pip安装Pyinstaller 。 注意,如果使用Pyinstaller,则应...

Python异步操作MySQL示例【使用aiomysql】

本文实例讲述了Python异步操作MySQL。分享给大家供大家参考,具体如下: 安装aiomysql 依赖 Python3.4+ asyncio PyMySQL 安装...

pandas值替换方法

如下所示: import pandas as pd from pandas import * import numpy as np data = Series([1,-999,...

Python Sqlite3以字典形式返回查询结果的实现方法

sqlite3本身并没有像pymysql一样原生提供字典形式的游标。 cursor = conn.cursor(pymysql.cursors.DictCursor) 但官方文...

利用python代码写的12306订票代码

本文实例讲述了python代码写的12306订票代码,分享给大家供大家参考。 具体实现方法如下: import datetime import json import re impo...