Py3.4 IMAPLib Login... 'str' does not support the buffer interface
<p>Using imaplib, I'm trying to connect to a mailserver.
When I include the password as just a normal string: 'password'
It connects fine.  But I'm trying to slightly obfuscate my password, so I previously had run it through b64encode, and then used b64decode in the login:</p>

<pre><code>#Works:
mail.login('myloginname', 'myPassword')

#Doesn't Work:
mail.login('myloginname', base64.b64decode('Ja3rHsnakhdgkhervc'))
# or
mail.login('myloginname', bytes(base64.b64decode('Ja3rHsnakhdgkhervc')))
</code></pre>

<p>...</p>

<pre><code>Traceback (most recent call last):
  File "./testing.py", line 15, in &lt;module&gt;
    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
</code></pre>

<p>Suggestions?</p>

<p>You are passing in a <code>bytes</code> object for the password, not a <code>str</code> value, because that's what <code>base64.b64decode()</code> returns.</p>

<p>You'd have to <em>decode</em> that value to a string:</p>

<pre><code> base64.b64decode('Ja3rHsnakhdgkhervc').decode('ascii')
</code></pre>

<p>The exception is caused by the <code>bytes.replace()</code> method, which expects <code>bytes</code> arguments. Since <code>'\\'</code> and <code>'\\\\'</code> are <code>str</code> objects, you get a traceback at <code>args.replace('\\', '\\\\')</code> only because <code>args</code> is a <code>bytes</code> object:</p>

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

python python-3.x base64 imaplib