解决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 ...
随机推荐
- InnoDB存储引擎的表空间文件,重做日志文件
存储引擎文件:因为MySQL表存储引擎的关系,每个存储引擎都会有自己的文件来保存各种数据.这些存储引擎真正存储了数据和索引等数据. 表空间文件 InnoDB存储引擎在存储设计上模仿了Oracle,将存 ...
- js中字符串的替换
定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...
- Eclipse项目红感叹号
原因:显示红色感叹号是因为jar包的路径不对 解决:在项目上右击Build Path -> Configure Build Paht...(或Propertise->Java Build ...
- Entity Framework 6.0 Code First(转)
源自:http://www.cnblogs.com/panchunting/tag/Code%20First/ 1 Conventions 2 Custom Conventions 3 Data An ...
- weblogic 异常常见处理方法
1.系统日志 通过分析weblogic的系统日志及应用程序出错的地方,可能找到异常的原因. 2. 数据库连接 如果用户访问系统过慢,且连接池已经占满而weblogic的线程数量很少,就要查看应用是否没 ...
- SSH安全登陆原理:密码登陆与公钥登陆
SSH全称(Secure SHell)是一种以安全性闻名的应用层网络通信协议,用于计算机间的安全通信,是目前比较成熟的远程登陆解决方案. 它提供两种方法登陆: 1.密码登陆 2.公钥登陆 密码 ...
- bug: 1.eclipse 中tomcat server 变灰色 2.build path 添加的jar不识别
1.网上百度的方案试过,没有作用. 最后删掉tomcat重新解压配置解决. 确保工程属性栏中,server正确才可加载tomcat. 猜测可能原因,tomcat在eclipse有时崩溃,一般做法是将s ...
- 事务,约束,范式,视图,索引,pl/sql
1.操作分类: DML. DDL. DCL manipulation definition control 2.transction 事务 起始于DML,遇到 commit ,rollb ...
- i.mx6 Android6.0.1分析input子系统:测试
getevent与sendevent工具 Android系统提供了getevent与sendevent两个工具供开发者从设备节点中直接读取输入事件或写入输入事件. 在这里,我们测试音量加和音量减按键 ...
- zabbix使用(自定义监控、自动报警)
自定义监控(制作模板) zabbix自带模板Template OS Linux (Template App Zabbix Agent)提供CPU.内存.磁盘.网卡等常规监控,只要新加主机关联此模板,就 ...