【爬坑】Python 3.6 在 Socket 编程时出现类型错误 TypeError: a bytes-like object is required, not 'str'

时间:2021-11-07 15:30:16

 

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 编程