问题:无法用json.loads()
解析数据。
现象:但是将收到的数据复制粘贴成字符串就可以接续出来。纠结了很久才发现,两个长度不一样。
str是看不出来的,于是就转换成了bytes,发现
收到的数据为:
<class ‘bytes’>
b’\r\n\r\n\x00\x00\x00\x00\x00\x00\x00\x00{\r\n\t"author": “app”\r\n}’
而复制出来的字符串没有\x00
原代码
self.data = self.request.recv(1024).decode('UTF-8', 'ignore').strip()
其中strip()
只能去掉\r,\\n,\t
,无法去掉\x00
。
解决办法:
在源码后添加strip(b'\x00'.decode())
即可。
最终代码:
self.data = self.request.recv(1024).decode('UTF-8', 'ignore').strip().strip(b'\x00'.decode())