python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
来源:互联网 发布:mac 8081端口被占用 编辑:程序博客网 时间:2024/05/18 05:11
首先我实现的功能是:使用tcp协议实现单线程的QQ聊天
代码如下:
from socket import *serversocket = socket(AF_INET, SOCK_STREAM)serversocket.bind(('',7788))serversocket.listen(5)while True:clientsocket,clientaddr = serversocket.accept()while True:recvdata = clientsocket.recv(1024)if len(recvdata)>0:print("%s:%s"%(str(clientaddr),recvdata))else:breaksenddata = input("请输入数据:")clientsocket.send(senddata)clientsocket.close()serversocket.close()
运行之后,出现如下错误:
python3 03-tcp单线程实现QQ聊天.py ('192.168.1.13', 52296):123请输入数据:asdfdTraceback (most recent call last): File "03-tcp单线程实现QQ聊天.py", line 21, in <module> clientsocket.send("senddata")TypeError: a bytes-like object is required, not 'str'
错误即为:类型错误:需要一个类似字节的对象,而不是str
经过查python3.5手册后,发现:
socket.send(bytes[, flags])
但是:
python中bytes和str两种类型转换的函数encode()、decode()
- str通过encode()方法可以编码为指定的bytes
- 如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法
将代码改正后:
clientsocket.send(senddata.encode())
阅读全文
0 0
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- python socket send 函数 报错:TypeError: a bytes-like object is required, not 'str'
- python3 TypeError: a bytes-like object is required, not 'str'
- python3 TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- 解决python3的pickle.load错误:a bytes-like object is required, not 'str'
- python中Base64编码和解码:TypeError: a bytes-like object is required, not 'str'
- python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- Python 3.x Error:TypeError: a bytes-like object is required, not 'str'
- Python中3+版本运行2+版本程序出现TypeError: a bytes-like object is required, not 'str'之读取PDF中的图片
- Python中“a bytes-like object is required, not 'str'”的问题
- a bytes-like object is required,not 'str'
- Error:a bytes-like object is required not str
- python3 套接字异常(a byte-like object is required not str)
- can't use a string pattern on a bytes-like object错误和must be str, not bytes错误
- 《算法艺术与信息学竞赛》之 贪心 例一 Gone Fishing
- Dorado 7 自动生成Dao的类
- LSTM循环神经网络中的超长序列问题
- TensorFlow conv2d原理及实践
- echarts在ionic3中动态渲染多种图表
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- 个性化推荐系统(三)---推荐系统意义一点思考
- android studio framework.jar
- 怎样一条SQL语句统计该年每月的数据个数
- EM(期望最大算法)在高斯混合模型中的python实现
- 高性能服务器编程-线程池
- 【ML】在梯度下降法中,误差函数是局部最小如何处理
- ajax回调json数据遍历取出数据
- 中断-中断的种类