1. 程式人生 > >【爬坑】Python 3.6 在 Socket 程式設計時出現型別錯誤 TypeError: a bytes-like object is required, not 'str'

【爬坑】Python 3.6 在 Socket 程式設計時出現型別錯誤 TypeError: a bytes-like object is required, not 'str'


 

1. 問題描述

  Python 3.6 在 Socket 程式設計時出現錯誤如下

Traceback (most recent call last):

  File "F:/share/IdeaProjects/test/mypython/test/test10_tcpclient.py", line 17, in <module>
    sock.send(str)
TypeError: a bytes-like object is required, not 'str'

Process finished with exit code 1

 


2. 解決方案

  型別錯誤,解決方案參考 python執行tcp伺服器和客戶端

  將 Client 端程式碼改為以下

sock.send(bytes(str,'utf-8'))

  詳情請檢視  [Python_7] Python Socket 程式設計