Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案
生活随笔
收集整理的這篇文章主要介紹了
Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運行環境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()方法:
轉載于:https://www.cnblogs.com/izhanglei/p/7590608.html
總結
以上是生活随笔為你收集整理的Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ots在线考计算机的word,ots在线
- 下一篇: python设置图片透明度_学习pyth