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

  • 相关阅读:
    uniapp版本迭代
    上传图像裁剪功能
    uniapp 复制到剪切板
    uniapp吸顶功能
    地图导航到目的地
    uniapp视频图片上传
    获取昨天今天明天的时间
    【VUE】 前端面试题小结
    vue获取当前时间 实时刷新
    CSS linear-gradient() 函数
  • 原文地址:https://www.cnblogs.com/Tempted/p/7428589.html
Copyright © 2011-2022 走看看