简体   繁体   English

从 Python3 中的 base64 编码字符串中删除新行“\\n”?

[英]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.我正在尝试在 Python3 中建立 HTTPS 连接,当我尝试对我的用户名和密码进行编码时, base64 encodebytes方法返回编码值,并在末尾添加一个换行符“\\n”,因此我得到了一个尝试连接时出错。

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?有没有办法告诉base64库在编码时不要附加换行符,或者删除这个换行符的最佳方法是什么? I tried using the replace method but I get the following error:我尝试使用replace方法,但出现以下错误:

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 .而不是encodestring考虑使用b64encode Later does not add \\n characters.后来不加\\n字符。 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它产生相同的编码字符串,除了\\n

以下代码将工作

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

For Python 3 use:对于 Python 3 使用:

binascii.b2a_base64(cipher_text, newline=False)

For Python 2 use:对于 Python 2 使用:

binascii.b2a_base64(cipher_text)[:-1]

I concur with Mandar's observation that base64.xxxx_encode() would produce output without line wrap \\n .我同意base64.xxxx_encode()的观察,即base64.xxxx_encode()将产生没有换行\\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. Python 3 文档确实提到base64.encode(...)会在每 76 个字节的输出后添加换行符。 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".相比之下,所有其他*_encode(...)函数根本没有提到它们的换行行为,这可以说是“无换行行为”。 For what it's worth, the Python 2 documentation does not mention anything about line wrap at all.就其价值而言, Python 2 文档根本没有提及有关换行的任何内容。

I applied @Harsh hint and these two functions to encode and decode binary data work for my application.我应用了@Harsh 提示和这两个函数来为我的应用程序编码和解码二进制数据工作。 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.我的要求是能够在 HTML src 元素和 CSS @font-face 语句中使用数据 URI 来表示二进制对象,特别是图像、声音和字体。 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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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