简体   繁体   中英

Remove the new line “\n” from base64 encoded strings in Python3?

I'm trying to make a HTTPS connection in Python3 and when I try to encode my username and password the base64 encodebytes method returns the encoded value with a new line character at the end "\\n" and because of this I'm getting an error when I try to connect.

Is there a way to tell the base64 library not to append a new line character when encoding or what is the best way to remove this new line character? I tried using the replace method but I get the following error:

Traceback (most recent call last):
  File "data_consumer.py", line 33, in <module>
    auth_base64 = auth_base64.replace('\n', '')
TypeError: expected bytes, bytearray or buffer compatible object

My code:

auth = b'username@domain.com:passWORD'
auth_base64 = base64.encodebytes(auth)
auth_base64 = auth_base64.replace('\n', '')

Any ideas? Thanks

Instead of encodestring consider using b64encode . Later does not add \\n characters. eg

In [11]: auth = b'username@domain.com:passWORD'

In [12]: base64.encodestring(auth)
Out[12]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA==\n'

In [13]: base64.b64encode(auth)
Out[13]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA=='

It produces identical encoded string except the \\n

以下代码将工作

auth_base64 = auth_base64.decode('utf-8').replace('\n', '')

For Python 3 use:

binascii.b2a_base64(cipher_text, newline=False)

For Python 2 use:

binascii.b2a_base64(cipher_text)[:-1]

I concur with Mandar's observation that base64.xxxx_encode() would produce output without line wrap \\n .

For those who want a more confident understanding than merely an observation, these are the official promise (sort of), that I can find on this topic. The Python 3 documentation does mention base64.encode(...) would add newlines after every 76 bytes of output. Comparing to that, all other *_encode(...) functions do not mention their linewrap behavior at all, which can argurably be considered as "no line wrap behavior". For what it's worth, the Python 2 documentation does not mention anything about line wrap at all.

I applied @Harsh hint and these two functions to encode and decode binary data work for my application. My requirement is to be able to use data URIs in HTML src elements and CSS @font-face statements to represent binary objects, specifically images, sounds and fonts. These functions work.

import binascii

def string_from_binary(binary): 
    return binascii.b2a_base64(binary, newline=False).decode('utf-8')

def string_to_binary(string): 
    return binascii.a2b_base64(string.encode('utf-8'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM