本文整理汇总了Python中email.base64mime.body_encode方法的典型用法代码示例。如果您正苦于以下问题:Python base64mime.body_encode方法的具体用法?Python base64mime.body_encode怎么用?Python base64mime.body_encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.base64mime
的用法示例。
在下文中一共展示了base64mime.body_encode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_encode
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def test_encode(self):
eq = self.assertEqual
eq(base64mime.body_encode(b''), b'')
eq(base64mime.body_encode(b'hello'), 'aGVsbG8=\n')
# Test the binary flag
eq(base64mime.body_encode(b'hello\n'), 'aGVsbG8K\n')
# Test the maxlinelen arg
eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40), """\
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg
eHh4eCB4eHh4IA==
""")
# Test the eol argument
eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40, eol='\r\n'),
"""\
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r
eHh4eCB4eHh4IA==\r
""")
示例2: _test_encode
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def _test_encode(self, body, expected_encoded_body, maxlinelen=None, eol=None):
kwargs = {}
if maxlinelen is None:
# Use body_encode's default.
maxlinelen = 76
else:
kwargs['maxlinelen'] = maxlinelen
if eol is None:
# Use body_encode's default.
eol = '\n'
else:
kwargs['eol'] = eol
encoded_body = quoprimime.body_encode(body, **kwargs)
self.assertEqual(encoded_body, expected_encoded_body)
if eol == '\n' or eol == '\r\n':
# We know how to split the result back into lines, so maxlinelen
# can be checked.
for line in encoded_body.splitlines():
self.assertLessEqual(len(line), maxlinelen)
示例3: auth
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def auth(self, mechanism, authobject, *, initial_response_ok=True):
"""Authentication command - requires response processing.
'mechanism' specifies which authentication mechanism is to
be used - the valid values are those listed in the 'auth'
element of 'esmtp_features'.
'authobject' must be a callable object taking a single argument:
data = authobject(challenge)
It will be called to process the server's challenge response; the
challenge argument it is passed will be a bytes. It should return
bytes data that will be base64 encoded and sent to the server.
Keyword arguments:
- initial_response_ok: Allow sending the RFC 4954 initial-response
to the AUTH command, if the authentication methods supports it.
"""
# RFC 4954 allows auth methods to provide an initial response. Not all
# methods support it. By definition, if they return something other
# than None when challenge is None, then they do. See issue #15014.
mechanism = mechanism.upper()
initial_response = (authobject() if initial_response_ok else None)
if initial_response is not None:
response = encode_base64(initial_response.encode('ascii'), eol='')
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
else:
(code, resp) = self.docmd("AUTH", mechanism)
# If server responds with a challenge, send the response.
if code == 334:
challenge = base64.decodebytes(resp)
response = encode_base64(
authobject(challenge).encode('ascii'), eol='')
(code, resp) = self.docmd(response)
if code in (235, 503):
return (code, resp)
raise SMTPAuthenticationError(code, resp)
示例4: auth
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def auth(self, mechanism, authobject, *, initial_response_ok=True):
"""Authentication command - requires response processing.
'mechanism' specifies which authentication mechanism is to
be used - the valid values are those listed in the 'auth'
element of 'esmtp_features'.
'authobject' must be a callable object taking a single argument:
data = authobject(challenge)
It will be called to process the server's challenge response; the
challenge argument it is passed will be a bytes. It should return
an ASCII string that will be base64 encoded and sent to the server.
Keyword arguments:
- initial_response_ok: Allow sending the RFC 4954 initial-response
to the AUTH command, if the authentication methods supports it.
"""
# RFC 4954 allows auth methods to provide an initial response. Not all
# methods support it. By definition, if they return something other
# than None when challenge is None, then they do. See issue #15014.
mechanism = mechanism.upper()
initial_response = (authobject() if initial_response_ok else None)
if initial_response is not None:
response = encode_base64(initial_response.encode('ascii'), eol='')
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
else:
(code, resp) = self.docmd("AUTH", mechanism)
# If server responds with a challenge, send the response.
if code == 334:
challenge = base64.decodebytes(resp)
response = encode_base64(
authobject(challenge).encode('ascii'), eol='')
(code, resp) = self.docmd(response)
if code in (235, 503):
return (code, resp)
raise SMTPAuthenticationError(code, resp)
示例5: test_len
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def test_len(self):
eq = self.assertEqual
eq(base64mime.header_length('hello'),
len(base64mime.body_encode(b'hello', eol='')))
for size in range(15):
if size == 0 : bsize = 0
elif size <= 3 : bsize = 4
elif size <= 6 : bsize = 8
elif size <= 9 : bsize = 12
elif size <= 12: bsize = 16
else : bsize = 20
eq(base64mime.header_length('x' * size), bsize)
示例6: test_encode_one_line_one_space
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def test_encode_one_line_one_space(self):
self._test_encode(' \n', '=20\n')
# XXX: body_encode() expect strings, but uses ord(char) from these strings
# to index into a 256-entry list. For code points above 255, this will fail.
# Should there be a check for 8-bit only ord() values in body, or at least
# a comment about the expected input?
示例7: login
# 需要导入模块: from email import base64mime [as 别名]
# 或者: from email.base64mime import body_encode [as 别名]
def login(self, username, password):
def encode_cram_md5(challenge, username, password):
challenge = base64.decodebytes(challenge)
response = username + " " + hmac.HMAC(password.encode('ascii'), challenge, 'md5').hexdigest()
return encode_base64(response.encode('ascii'), eol="")
def encode_plain(user, password):
return encode_base64(("\0%s\0%s" % (user, password)).encode('ascii'), eol="")
AUTH_PLAIN = "PLAIN"
AUTH_CRAM_MD5 = "CRAM-MD5"
AUTH_LOGIN = "LOGIN"
yield self.ehlo_or_helo_if_needed()
if not self.has_extn('auth'):
raise smtplib.SMTPException("SMTP Auth extension not supported by server ")
advertised_authlist = self.esmtp_features["auth"].split()
preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
authlist = [auth for auth in preferred_auths if auth in advertised_authlist]
if not authlist:
raise smtplib.SMTPException("No suitable authentication method found.")
# Some servers advertise authentication methods they don't really
# support, so if authentication fails, we continue until we've tried
# all methods.
for authmethod in authlist:
if authmethod == AUTH_CRAM_MD5:
(code, resp) = yield self.docmd(b'AUTH', AUTH_CRAM_MD5)
if code == 334:
(code, resp) =yield self.docmd(encode_cram_md5(resp, username, password).encode('ascii'))
elif authmethod == AUTH_PLAIN:
(code, resp) =yield self.docmd(b'AUTH',
(AUTH_PLAIN + " " + encode_plain(username, password)).encode('ascii'))
elif authmethod == AUTH_LOGIN:
(code, resp) = yield self.docmd(b'AUTH',
("%s %s" % (AUTH_LOGIN, encode_base64(username.encode('ascii'), eol=''))).encode('ascii'))
if code == 334:
(code, resp) = yield self.docmd(encode_base64(password.encode('ascii'), eol='').encode('ascii'))
# 235 == 'Authentication successful'
# 503 == 'Error: already authenticated'
if code in (235, 503):
return (code, resp)
# We could not login sucessfully. Return result of last attempt.
raise smtplib.SMTPAuthenticationError(code, resp)