UnicodeDecodeError:'utf-8' codec can't decode byte 0xc2

  • 故障代码:

data = json.loads(rec.decode())   # rec 为client发送的json序列
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 88: invalid continuation byte

  •  故障环境

socket、Ftp试验:

client传输文件时,server端报出错误,client端随后ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。

  • 故障分析:

print(data) 会发现,data中除仅需要的数据外,还包含了其他未知数据。

正是这部分数据,在 .decode()时,发生了错误。

  • 故障原因

client两次send的数据,在缓冲区粘在了一起。被server端一次性接收。

  • 故障解决

办法1:client端在第一次send后,应该清空缓冲区

办法2:client端在第一次send后,延时等待下一次发送

办法3:client端在第一次send后,进行一次接收。与server端进行一次通信

posted @ 2017-08-13 12:01  HelloLx  阅读(2358)  评论(0编辑  收藏  举报