如何将字节流转换为字符串?

时间:2019-05-14 17:33:47

标签: python-3.x

如何将字节流(socket.recv())转换为字符串,或者应该如何解码?

我想建立与服务器的连接,但是当我尝试解码recv()字节流时,我得到了UnicodeDecodeError,我可以通过添加ignore来解决,以解码,但是随后我获得了utf-8的字符串,没读过(也许utf-8是错误的编解码器,但是我怎么知道我必须使用哪个?)

这就是我从recv()得到的

b'HTTP/1.1 200 OK\r\nDate: Tue, 14 May 2019 17:28:11 GMT\r\nServer: Apache/2.2.22 (Debian)\r\nAccept-Encoding\r\nContent-Encoding: gzip\r\nContent-Length: 5934\r\nKeep-Alive: timeout=2, max=5\r\nConnection: Keep-Alive\r\nContent-Type: text/html;charset=utf-8\r\n\r\n\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed]\
..
.a little bit more bytes 
...
 #3\x81\x15ntT   (at the end)

0 个答案:

没有答案