解决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\r\n\r\n")
    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\r\n\r\n")
    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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM