在python中解码字符串时发生base64错误
我正在开发一个可以发送图像的聊天室。但是图像很大,所以我将它们分成一部分发送,并将每个部分添加到一个字符串中。当我试图解码包含信息的字符串时,我得到以下错误:在python中解码字符串时发生base64错误,python,base64,syntax-error,Python,Base64,Syntax Error,我正在开发一个可以发送图像的聊天室。但是图像很大,所以我将它们分成一部分发送,并将每个部分添加到一个字符串中。当我试图解码包含信息的字符串时,我得到以下错误: return binascii.a2b_base64(s) Error: Incorrect padding 这是我的密码: def getData(self): chatArea = self.chatArea imageBytes = "" imageMode = False while 1:
return binascii.a2b_base64(s)
Error: Incorrect padding
这是我的密码:
def getData(self):
chatArea = self.chatArea
imageBytes = ""
imageMode = False
while 1:
data = self.s.recv(8000)
if not data:
break
if imageMode == True:
imageBytes = imageBytes + data
if data[-1] == ")":
newImage = open("Untitled.png", "wb")
newImage.write(imageBytes.decode("base64"))
newImage.close()
imageMode = False
print("Done")
else:
if re.findall(r'\[(.*?)\]', data) == ["Image"]:
print("Got the data")
imageMode = True
else:
string = data + "\n\n"
chatArea.configure(state=NORMAL)
chatArea.insert(END, string)
chatArea.configure(state=DISABLED)
newString = string.split(":")[0]
self.chatArea.see(END)
if newString == self.myName or newString == "Server":
pass
else:
winsound.PlaySound("Notify.wav", winsound.SND_FILENAME)
为什么在尝试创建图像时会出现此错误?
如何修复它?如果数据[-1]==”:
似乎希望base64编码字符串以一个右括号结束
这可能是你的问题的原因。imageBytes数据是否有需要修剪的尾随或前导协议数据
希望这能有所帮助。@howaboutn我不是在试图忽略错误,而是在试图找到防止错误的方法。为什么它会给我这个错误。有人有解决方案吗?当编码字符不是4的倍数时,Base64需要填充。与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。请参见“.BTW,它是“提前感谢”,而不是“提前感谢”。是的,我在字符串的末尾添加了a)。这会导致错误吗?是的,我想是的,如果内存可用,您可以在base64编码的数据中插入空格,但其他很少。请尝试
imageBytes=imageBytes.rstrip(“)