在python3中输入以下代码:
可以发现有以下报错:
根据错误,做以下修改可以得到结果:
知识点
b = b"example" # bytes object
s = "example" # str object
sb = bytes(s, encoding = "utf8") # str to bytes
或者:sb = str.encode(s) # str to bytes
bs = str(b, encoding = "utf8") # bytes to str
或者:bs = bytes.decode(b) # bytes to str
出现此错误的原因是:
python3和Python2在套接字返回值解码上有区别,也就是说报错的这段代码在python2上可以正常使用。