TypeError: expected bytes, not str

import base64
import io
a = "<urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>"
b = base64.b64encode(bytes(a,'gb2312')) # 对字符串编码  URLError(ConnectionRefusedError(10061, '由于目标计算机积极拒绝,无法连接。', None, 10061),) this is a test   <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>
print ('b11:',b)
print ('b12:',base64.b64decode(b) )# 对字符串解码
c = io.StringIO()
c.write(a)
d = io.StringIO()
e = io.StringIO()
c.seek(0)
print(c, d)
#s1=str(c)
print(type(c))
base64.b64encode(bytes(str(c),'gb2312'))# 对StringIO内的数据进行编码
print (d.getvalue())
d.seek(0)
base64.decode(d, e) # 对StringIO内的数据进行解码
print (e.getvalue())
a = "this is a +test"
b = base64.urlsafe_b64encode(a) # 进行url的字符串编码
 
运行代码后后,b = base64.urlsafe_b64encode(a)处报错 TypeError: expected bytes, not str
代码修改如下:
import base64
import io
a = "<urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>"
b = base64.b64encode(bytes(a,'gb2312')) # 对字符串编码  URLError(ConnectionRefusedError(10061, '由于目标计算机积极拒绝,无法连接。', None, 10061),) this is a test   <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>
print ('b11:',b)
print ('b12:',base64.b64decode(b) )# 对字符串解码
c = io.StringIO()
c.write(a)
d = io.StringIO()
e = io.StringIO()
c.seek(0)
print(c, d)
#s1=str(c)
print(type(c))
base64.b64encode(bytes(str(c),'gb2312'))# 对StringIO内的数据进行编码
print (d.getvalue())
d.seek(0)
base64.decode(d, e) # 对StringIO内的数据进行解码
print (e.getvalue())
a = "this is a +test"
b = base64.urlsafe_b64encode(bytes(a,'gb2312'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值