解决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
解决python3 UnicodeDecodeError: 'gbk' codec can't decode byte的更多相关文章
- 【错误解决】UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 840: illegal multibyte sequence
原文来源:https://www.zhihu.com/question/22699590 编码问题错误,读入文件的时候指定编码即可. with open(fname, encoding='utf-8' ...
- python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position
python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 实用python的时候 打开一个csv的文件出 ...
- python3安装xadmin出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 3444: illegal multibyte sequence
python3的环境安装xadmin时,直接pip install xadmin出现 Downloading xadmin-0.6.1.tar.gz (1.0MB) 100% |███████████ ...
- python UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 87: illegal multibyte sequence异常解决
我们处理文本文件时,经常会遇到这样的报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 87: illegal ...
- 14 python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xb7 in position 26: illegal multibyte sequence解决方法
>>> f = open("D:\\all.txt", "r")>>> f.read()Traceback (most re ...
- Django 运行Admin 页面时出现 UnicodeDecodeError: 'gbk' codec can't decode byte XXXX解决方法
具体报错信息 Traceback (most recent call last): File "D:\Anaconda3\lib\site-packages\django\core\hand ...
- 启动运行python3时 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 170: illegal multibyte sequence
重现 在cmd中输入Python,运行后,出现以下错误: Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal m ...
- 针对django2.2报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: ill....
1.报错: File "D:\Python\Python37-32\lib\site-packages\django\views\debug.py", line 332, in g ...
随机推荐
- 介绍nodejs中的path模块的几个方法
webpack中常用的: var path = require('path') 是nodejs中的path模块,介绍一下webpack中常用的几个path模块的方法: 应用node环境的时候,这个pa ...
- 3-0 js基础 语言特性及性能优化
1.语言特性: 内存泄露:内存没有释放,越堆越多. 垃圾回收(生命周期): 1.局部 很短 在局部中当函数完成时.已经释放了.全局变量在页面关闭的时候才被回收. 2.全局 很长 3.闭包.可长可短,只 ...
- HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)
Elven Postman Elves are very peculiar creatures. As we all know, they can live for a very long time ...
- R语言统计字符串的字符数ncahr函数
函数计算字符数量,包括在一个字符串的空格的个数. 语法 nchar()函数的基本语法是: nchar(x) 以下是所使用的参数的说明: x - 向量输入. 示例 result <- nchar( ...
- [转]OData and Authentication – Part 5 – Custom HttpModules
本文转自:https://blogs.msdn.microsoft.com/odatateam/2010/07/19/odata-and-authentication-part-5-custom-ht ...
- c#配置文件app.config 与 Settings.settings
本篇博客将介绍C#中Settings的使用.参考:https://docs.microsoft.com/zh-cn/visualstudio/ide/managing-application-sett ...
- jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
如果你看了上一篇<ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)>的话,应该就知道我是逼不得已要认真学 ...
- MyBatis别名
Spring的别名管理比较规范,有严格的接口规范,SimpleAliasRegistry实现 -> AliasRegistry接口,而且是线程安全的,Map也用的是ConcurrentHashM ...
- The configuration section 'system.serviceModel' cannot be read because it is missing a section decla
将Asp.Net 2.0的Web Site搭建在IIS7(7.5)上时,运行出现500.19错误, 错误提示为 The configuration section 'system.serviceMod ...
- Redis ,Memcached ,Mongodb 对比
memcached: 1.适合做内存缓存,对可靠性没有要求,不支持持久化:速度快.并发高.2.支持的数据结构单一,只支持Key-value,3.value最大支持1M3.在传统tomcat 部署war ...