python base64 decode incorrect padding错误解决方法

公园里的草真绿啊,绿得让你感觉那是一块绿地毯;公园里的草真密啊,密得看不见泥土;公园里的草真柔啊,微风一吹,它们就翩翩起舞。

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 base64 decode incorrect padding错误解决方法到此结束。幽幽的深谷显的骇人的清静和阴冷。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python3内置模块之json编码解码方法详解