Py3.4 IMAPLib登录…'str'不支持中的缓冲区

2024-10-03 09:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

使用imaplib,我正在尝试连接到邮件服务器。 当我将密码作为普通字符串包含时:“password” 连接良好。但是我正在尝试稍微混淆我的密码,所以我之前已经通过b64encode运行了它,然后在登录中使用了b64decode:

#Works:
mail.login('myloginname', 'myPassword')

#Doesn't Work:
mail.login('myloginname', base64.b64decode('Ja3rHsnakhdgkhervc'))
# or
mail.login('myloginname', bytes(base64.b64decode('Ja3rHsnakhdgkhervc')))

。。。你知道吗

Traceback (most recent call last):
  File "./testing.py", line 15, in <module>
    mail.login('myloginname', bytes(base64.b64decode('Ja3rHsnakhdgkhervc')))
  File "/usr/local/lib/python3.4/imaplib.py", line 536, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/local/lib/python3.4/imaplib.py", line 1125, in _quote
    arg = arg.replace('\\', '\\\\')
TypeError: 'str' does not support the buffer interface

建议?你知道吗


Tags: inpy密码bytesusrlineloginmail
1条回答
网友
1楼 · 发布于 2024-10-03 09:15:38

您传递的是一个bytes对象作为密码,而不是str值,因为base64.b64decode()会返回这个值。你知道吗

您必须值解码为字符串:

 base64.b64decode('Ja3rHsnakhdgkhervc').decode('ascii')

异常是由bytes.replace()方法引起的,该方法需要bytes参数。因为'\\''\\\\'str对象,所以在args.replace('\\', '\\\\')得到回溯仅仅是因为argsbytes对象:

>>> b'foo'.replace('\\', '\\\\')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> 'foo'.replace('\\', '\\\\')
'foo'

相关问题 更多 >