首页 > 编程语言 > 详细

Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案

时间:2017-09-25 10:25:18      阅读:622      评论:0      收藏:0      [点我收藏+]

运行环境Mac  Python 3.5.2

Q:

http_response = """\

HTTP/1.1 200 OK

 

Hello, World!

"""

client_connection.sendall(http_response)

 

TypeError: a bytes-like object is required, not ‘str‘

类型错误,需要的是一个byte类型,不是str类型

A:

http_response = """\

HTTP/1.1 200 OK

 

Hello, World!

"""

encode_http_response = http_response.encode()

client_connection.sendall(encode_http_response)

 

Expand:

str通过encode()方法可以编码为指定的bytes

反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法:

Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案

原文:http://www.cnblogs.com/izhanglei/p/7590608.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!