本文整理汇总了Python中email.errors.InvalidBase64CharactersDefect方法的典型用法代码示例。如果您正苦于以下问题:Python errors.InvalidBase64CharactersDefect方法的具体用法?Python errors.InvalidBase64CharactersDefect怎么用?Python errors.InvalidBase64CharactersDefect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.errors
的用法示例。
在下文中一共展示了errors.InvalidBase64CharactersDefect方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode_b
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def decode_b(encoded):
defects = []
pad_err = len(encoded) % 4
if pad_err:
defects.append(errors.InvalidBase64PaddingDefect())
padded_encoded = encoded + b'==='[:4-pad_err]
else:
padded_encoded = encoded
try:
return base64.b64decode(padded_encoded, validate=True), defects
except binascii.Error:
# Since we had correct padding, this must an invalid char error.
defects = [errors.InvalidBase64CharactersDefect()]
# The non-alphabet characters are ignored as far as padding
# goes, but we don't know how many there are. So we'll just
# try various padding lengths until something works.
for i in 0, 1, 2, 3:
try:
return base64.b64decode(encoded+b'='*i, validate=False), defects
except binascii.Error:
if i==0:
defects.append(errors.InvalidBase64PaddingDefect())
else:
# This should never happen.
raise AssertionError("unexpected binascii.Error")
示例2: test_invalid_chars_in_base64_payload
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_invalid_chars_in_base64_payload(self):
source = textwrap.dedent("""\
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
dm\x01k===
""")
msg = self._str_msg(source)
with self._raise_point(errors.InvalidBase64CharactersDefect):
payload = msg.get_payload(decode=True)
if self.raise_expected: return
self.assertEqual(payload, b'vi')
self.assertDefectsEqual(self.get_defects(msg),
[errors.InvalidBase64CharactersDefect])
示例3: test_invalid_character
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_invalid_character(self):
self._test(b'dm\x01k===', b'vi', [errors.InvalidBase64CharactersDefect])
示例4: test_invalid_character_and_bad_padding
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_invalid_character_and_bad_padding(self):
self._test(b'dm\x01k', b'vi', [errors.InvalidBase64CharactersDefect,
errors.InvalidBase64PaddingDefect])
示例5: test_b_undecodable_bytes_ignored_with_defect
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_b_undecodable_bytes_ignored_with_defect(self):
self._test(b'=?us-ascii?b?dm\xACk?='.decode('us-ascii',
'surrogateescape'),
'vi',
defects = [
errors.InvalidBase64CharactersDefect,
errors.InvalidBase64PaddingDefect])
示例6: test_b_invalid_bytes_ignored_with_defect
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_b_invalid_bytes_ignored_with_defect(self):
self._test('=?us-ascii?b?dm\x01k===?=',
'vi',
defects = [errors.InvalidBase64CharactersDefect])
示例7: test_get_unstructured_invalid_base64_character
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_get_unstructured_invalid_base64_character(self):
self._test_get_x(self._get_unst,
'=?utf-8?b?dm\x01k===?=',
'vi',
'vi',
[errors.InvalidBase64CharactersDefect],
'')
示例8: test_get_unstructured_invalid_base64_character_and_bad_padding
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_get_unstructured_invalid_base64_character_and_bad_padding(self):
self._test_get_x(self._get_unst,
'=?utf-8?b?dm\x01k?=',
'vi',
'vi',
[errors.InvalidBase64CharactersDefect,
errors.InvalidBase64PaddingDefect],
'')
示例9: decode_b
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def decode_b(encoded):
# First try encoding with validate=True, fixing the padding if needed.
# This will succeed only if encoded includes no invalid characters.
pad_err = len(encoded) % 4
missing_padding = b'==='[:4-pad_err] if pad_err else b''
try:
return (
base64.b64decode(encoded + missing_padding, validate=True),
[errors.InvalidBase64PaddingDefect()] if pad_err else [],
)
except binascii.Error:
# Since we had correct padding, this is likely an invalid char error.
#
# The non-alphabet characters are ignored as far as padding
# goes, but we don't know how many there are. So try without adding
# padding to see if it works.
try:
return (
base64.b64decode(encoded, validate=False),
[errors.InvalidBase64CharactersDefect()],
)
except binascii.Error:
# Add as much padding as could possibly be necessary (extra padding
# is ignored).
try:
return (
base64.b64decode(encoded + b'==', validate=False),
[errors.InvalidBase64CharactersDefect(),
errors.InvalidBase64PaddingDefect()],
)
except binascii.Error:
# This only happens when the encoded string's length is 1 more
# than a multiple of 4, which is invalid.
#
# bpo-27397: Just return the encoded string since there's no
# way to decode.
return encoded, [errors.InvalidBase64LengthDefect()]
示例10: test_broken_base64_payload
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_broken_base64_payload(self):
x = 'AwDp0P7//y6LwKEAcPa/6Q=9'
msg = Message()
msg['content-type'] = 'audio/x-midi'
msg['content-transfer-encoding'] = 'base64'
msg.set_payload(x)
self.assertEqual(msg.get_payload(decode=True),
(b'\x03\x00\xe9\xd0\xfe\xff\xff.\x8b\xc0'
b'\xa1\x00p\xf6\xbf\xe9\x0f'))
self.assertIsInstance(msg.defects[0],
errors.InvalidBase64CharactersDefect)
示例11: test_8bit_in_base64_body
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_8bit_in_base64_body(self):
# If we get 8bit bytes in a base64 body, we can just ignore them
# as being outside the base64 alphabet and decode anyway. But
# we register a defect.
m = self.bodytest_msg.format(charset='utf-8',
cte='base64',
bodyline='cMO2c3RhbAá=').encode('utf-8')
msg = email.message_from_bytes(m)
self.assertEqual(msg.get_payload(decode=True),
'pöstal'.encode('utf-8'))
self.assertIsInstance(msg.defects[0],
errors.InvalidBase64CharactersDefect)
示例12: test_b_invalid_bytes_incorrect_padding
# 需要导入模块: from email import errors [as 别名]
# 或者: from email.errors import InvalidBase64CharactersDefect [as 别名]
def test_b_invalid_bytes_incorrect_padding(self):
self._test('=?us-ascii?b?dm\x01k?=',
'vi',
defects = [
errors.InvalidBase64CharactersDefect,
errors.InvalidBase64PaddingDefect])