首页 > 编程语言 > 详细

python3 TypeError: a bytes-like object is required

时间:2018-03-23 14:06:44      阅读:349      评论:0      收藏:0      [点我收藏+]
运行telnetlib的时候报错:TypeError: a bytes-like object is required, not ‘str’,原因是因为python2和python3的版本的差异。
在python2中可正常运行,而python3最重要的新特性也是对文本和二进制数据做了更清晰的区分。文本用unicode编码,为str类型,二进制数据则为bytes类型。

python有两种类型转换的函数encode(),decode() 。

encode(编码),可以将str类型编码为bytes。
decode(译码),可以将bytes类型转换为str类型。

技术分享图片

因此在telnetlib交互的时候需要使用encode()。

tn.read_until("username:".encode())

python3 TypeError: a bytes-like object is required

原文:http://blog.51cto.com/juispan/2090259

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