本文整理匯總了Python中email.errors.InvalidBase64PaddingDefect方法的典型用法代碼示例。如果您正苦於以下問題:Python errors.InvalidBase64PaddingDefect方法的具體用法?Python errors.InvalidBase64PaddingDefect怎麽用?Python errors.InvalidBase64PaddingDefect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類email.errors
的用法示例。
在下文中一共展示了errors.InvalidBase64PaddingDefect方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: decode_b
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [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_bad_padding_in_base64_payload
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [as 別名]
def test_bad_padding_in_base64_payload(self):
source = textwrap.dedent("""\
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
dmk
""")
msg = self._str_msg(source)
with self._raise_point(errors.InvalidBase64PaddingDefect):
payload = msg.get_payload(decode=True)
if self.raise_expected: return
self.assertEqual(payload, b'vi')
self.assertDefectsEqual(self.get_defects(msg),
[errors.InvalidBase64PaddingDefect])
示例3: test_missing_padding
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [as 別名]
def test_missing_padding(self):
self._test(b'dmk', b'vi', [errors.InvalidBase64PaddingDefect])
示例4: test_invalid_character_and_bad_padding
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [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 InvalidBase64PaddingDefect [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_incorrect_padding
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [as 別名]
def test_b_invalid_bytes_incorrect_padding(self):
self._test('=?us-ascii?b?dm\x01k?=',
'vi',
defects = [
errors.InvalidBase64CharactersDefect,
errors.InvalidBase64PaddingDefect])
示例7: test_get_unstructured_missing_base64_padding
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [as 別名]
def test_get_unstructured_missing_base64_padding(self):
self._test_get_x(self._get_unst,
'=?utf-8?b?dmk?=',
'vi',
'vi',
[errors.InvalidBase64PaddingDefect],
'')
示例8: test_get_unstructured_invalid_base64_character_and_bad_padding
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [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 InvalidBase64PaddingDefect [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_missing_padding
# 需要導入模塊: from email import errors [as 別名]
# 或者: from email.errors import InvalidBase64PaddingDefect [as 別名]
def test_missing_padding(self):
# 1 missing padding character
self._test(b'dmk', b'vi', [errors.InvalidBase64PaddingDefect])
# 2 missing padding characters
self._test(b'dg', b'v', [errors.InvalidBase64PaddingDefect])