解决python3 UnicodeDecodeError: 'gbk' codec can't decode byte

本来想写个html来玩玩,结果读取文件得时候就BUG了。。。。

以下代码读取html中无中文没有问题。

def handle_request(client):
    buf = client.recv(1024)
    client.send(b"HTTP/1.1 200 OK

")
    with open ('index.html','r') as f:
        data = f.read()
    data=data.encode(encoding="utf8")
    #print(type(data))
    client.send(data)

添加中文。

报错信息如下:

UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in position 121: illegal multibyte sequence

解决方法:

把 open 的方式变为 二进制  with open ('index.html','rb') as f:

import  socket


def handle_request(client):
    buf = client.recv(1024)
    client.send(b"HTTP/1.1 200 OK

")
    with open ('index.html','rb') as f:
        data = f.read().decode('utf-8')
    data=data.encode(encoding="utf8")
    #print(type(data))
    client.send(data)

def main():
    s = socket.socket()
    s.bind(("localhost",9999))
    s.listen(5)

    while True:
        connection,address = s.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':
    main()

参考 decode 和 encode的用法:

http://blog.chinaunix.net/uid-27838438-id-4227131.html

原文地址:https://www.cnblogs.com/Tempted/p/7428589.html