zoukankan      html  css  js  c++  java
  • 解决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

  • 相关阅读:
    关于firstChild,firstElementChild和children
    trim(),正则表达式中文匹配
    Shell之基本用法
    Samba服务部署
    Linux基础(3)
    linux基础(2)
    Linux基础(1)
    网络基础及网络协议
    操作系统简介
    为何要学习计算机基础
  • 原文地址:https://www.cnblogs.com/Tempted/p/7428589.html
Copyright © 2011-2022 走看看