本文整理汇总了Python中email.encoders.encode_7or8bit方法的典型用法代码示例。如果您正苦于以下问题:Python encoders.encode_7or8bit方法的具体用法?Python encoders.encode_7or8bit怎么用?Python encoders.encode_7or8bit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.encoders
的用法示例。
在下文中一共展示了encoders.encode_7or8bit方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_binary_body_with_encode_7or8bit
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def test_binary_body_with_encode_7or8bit(self):
# Issue 17171.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit)
# Treated as a string, this will be invalid code points.
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg.get_payload(decode=True), bytesdata)
self.assertEqual(msg['Content-Transfer-Encoding'], '8bit')
s = StringIO()
g = Generator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_string(wireform)
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit')
示例2: test_get_body_encoding_with_uppercase_charset
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def test_get_body_encoding_with_uppercase_charset(self):
eq = self.assertEqual
msg = Message()
msg['Content-Type'] = 'text/plain; charset=UTF-8'
eq(msg['content-type'], 'text/plain; charset=UTF-8')
charsets = msg.get_charsets()
eq(len(charsets), 1)
eq(charsets[0], 'utf-8')
charset = Charset(charsets[0])
eq(charset.get_body_encoding(), 'base64')
msg.set_payload('hello world', charset=charset)
eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n')
eq(msg.get_payload(decode=True), 'hello world')
eq(msg['content-transfer-encoding'], 'base64')
# Try another one
msg = Message()
msg['Content-Type'] = 'text/plain; charset="US-ASCII"'
charsets = msg.get_charsets()
eq(len(charsets), 1)
eq(charsets[0], 'us-ascii')
charset = Charset(charsets[0])
eq(charset.get_body_encoding(), encoders.encode_7or8bit)
msg.set_payload('hello world', charset=charset)
eq(msg.get_payload(), 'hello world')
eq(msg['content-transfer-encoding'], '7bit')
示例3: test_binary_body_with_encode_7or8bit
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def test_binary_body_with_encode_7or8bit(self):
# Issue 17171.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit)
# Treated as a string, this will be invalid code points.
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
self.assertEqual(msg.get_payload(decode=True), bytesdata)
self.assertEqual(msg['Content-Transfer-Encoding'], '8bit')
s = BytesIO()
g = BytesGenerator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_bytes(wireform)
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit')
示例4: test_get_body_encoding_with_uppercase_charset
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def test_get_body_encoding_with_uppercase_charset(self):
eq = self.assertEqual
msg = Message()
msg['Content-Type'] = 'text/plain; charset=UTF-8'
eq(msg['content-type'], 'text/plain; charset=UTF-8')
charsets = msg.get_charsets()
eq(len(charsets), 1)
eq(charsets[0], 'utf-8')
charset = Charset(charsets[0])
eq(charset.get_body_encoding(), 'base64')
msg.set_payload(b'hello world', charset=charset)
eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n')
eq(msg.get_payload(decode=True), b'hello world')
eq(msg['content-transfer-encoding'], 'base64')
# Try another one
msg = Message()
msg['Content-Type'] = 'text/plain; charset="US-ASCII"'
charsets = msg.get_charsets()
eq(len(charsets), 1)
eq(charsets[0], 'us-ascii')
charset = Charset(charsets[0])
eq(charset.get_body_encoding(), encoders.encode_7or8bit)
msg.set_payload('hello world', charset=charset)
eq(msg.get_payload(), 'hello world')
eq(msg['content-transfer-encoding'], '7bit')
示例5: prepare_pgp_message
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def prepare_pgp_message(orig_msg: Message, pgp_fingerprint: str):
msg = MIMEMultipart("encrypted", protocol="application/pgp-encrypted")
# copy all headers from original message except all standard MIME headers
for i in reversed(range(len(orig_msg._headers))):
header_name = orig_msg._headers[i][0].lower()
if header_name.lower() not in _MIME_HEADERS:
msg[header_name] = orig_msg._headers[i][1]
# Delete unnecessary headers in orig_msg except to save space
delete_all_headers_except(
orig_msg, _MIME_HEADERS,
)
first = MIMEApplication(
_subtype="pgp-encrypted", _encoder=encoders.encode_7or8bit, _data=""
)
first.set_payload("Version: 1")
msg.attach(first)
second = MIMEApplication("octet-stream", _encoder=encoders.encode_7or8bit)
second.add_header("Content-Disposition", "inline")
# encrypt original message
encrypted_data = pgp_utils.encrypt_file(
BytesIO(orig_msg.as_bytes()), pgp_fingerprint
)
second.set_payload(encrypted_data)
msg.attach(second)
return msg
示例6: __init__
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def __init__(self, version=1):
data = "Version: %d" % version
MIMEApplication.__init__(self, data, 'pgp-encrypted',
_encoder=encode_7or8bit)
示例7: _encrypt_and_sign
# 需要导入模块: from email import encoders [as 别名]
# 或者: from email.encoders import encode_7or8bit [as 别名]
def _encrypt_and_sign(self, origmsg, encrypt_address, sign_address,
fetch_remote=True):
"""
Create an RFC 3156 compliang PGP encrypted and signed message using
C{encrypt_address} to encrypt and C{sign_address} to sign.
:param origmsg: The original message
:type origmsg: email.message.Message
:param encrypt_address: The address used to encrypt the message.
:type encrypt_address: str
:param sign_address: The address used to sign the message.
:type sign_address: str
:return: A Deferred with the MultipartEncrypted message
:rtype: Deferred
"""
# create new multipart/encrypted message with 'pgp-encrypted' protocol
def encrypt(res):
newmsg, origmsg = res
d = self._keymanager.encrypt(
origmsg.as_string(unixfrom=False),
encrypt_address, sign=sign_address,
fetch_remote=fetch_remote)
d.addCallback(lambda encstr: (newmsg, encstr))
return d
def create_encrypted_message(res):
newmsg, encstr = res
encmsg = MIMEApplication(
encstr, _subtype='octet-stream', _encoder=encode_7or8bit)
encmsg.add_header('content-disposition', 'attachment',
filename='msg.asc')
# create meta message
metamsg = PGPEncrypted()
metamsg.add_header('Content-Disposition', 'attachment')
# attach pgp message parts to new message
newmsg.attach(metamsg)
newmsg.attach(encmsg)
return newmsg
d = self._fix_headers(
origmsg,
MultipartEncrypted('application/pgp-encrypted'),
sign_address)
d.addCallback(encrypt)
d.addCallback(create_encrypted_message)
return d