本文整理汇总了Python中email.generator.BytesGenerator方法的典型用法代码示例。如果您正苦于以下问题:Python generator.BytesGenerator方法的具体用法?Python generator.BytesGenerator怎么用?Python generator.BytesGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.generator
的用法示例。
在下文中一共展示了generator.BytesGenerator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_smtputf8_policy
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_smtputf8_policy(self):
msg = EmailMessage()
msg['From'] = "Páolo <főo@bar.com>"
msg['To'] = 'Dinsdale'
msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
msg.set_content("oh là là, know what I mean, know what I mean?")
expected = textwrap.dedent("""\
From: Páolo <főo@bar.com>
To: Dinsdale
Subject: Nudge nudge, wink, wink \u1F609
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
oh là là, know what I mean, know what I mean?
""").encode('utf-8').replace(b'\n', b'\r\n')
s = io.BytesIO()
g = BytesGenerator(s, policy=policy.SMTPUTF8)
g.flatten(msg)
self.assertEqual(s.getvalue(), expected)
示例2: test_mangled_from_with_bad_bytes
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_mangled_from_with_bad_bytes(self):
source = textwrap.dedent("""\
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
From: aaa@bbb.org
""").encode('utf-8')
msg = email.message_from_bytes(source + b'From R\xc3\xb6lli\n')
b = BytesIO()
g = BytesGenerator(b, mangle_from_=True)
g.flatten(msg)
self.assertEqual(b.getvalue(), source + b'>From R\xc3\xb6lli\n')
# Test the basic MIMEAudio class
示例3: test_binary_body_with_encode_7or8bit
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [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: encode_multipart_message
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def encode_multipart_message(message):
# The message must be multipart.
assert message.is_multipart()
# The body length cannot yet be known.
assert "Content-Length" not in message
# So line-endings can be fixed-up later on, component payloads must have
# no Content-Length and their Content-Transfer-Encoding must be base64
# (and not quoted-printable, which Django doesn't appear to understand).
for part in message.get_payload():
assert "Content-Length" not in part
assert part["Content-Transfer-Encoding"] == "base64"
# Flatten the message without headers.
buf = BytesIO()
generator = BytesGenerator(buf, False) # Don't mangle "^From".
generator._write_headers = lambda self: None # Ignore.
generator.flatten(message)
# Ensure the body has CRLF-delimited lines. See
# http://bugs.python.org/issue1349106.
body = b"\r\n".join(buf.getvalue().splitlines())
# Only now is it safe to set the content length.
message.add_header("Content-Length", "%d" % len(body))
return message.items(), body
示例5: as_bytes
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def as_bytes(self, unixfrom=False, policy=None):
"""Return the entire formatted message as a bytes object.
Optional 'unixfrom', when true, means include the Unix From_ envelope
header. 'policy' is passed to the BytesGenerator instance used to
serialize the message; if not specified the policy associated with
the message instance is used.
"""
from email.generator import BytesGenerator
policy = self.policy if policy is None else policy
fp = BytesIO()
g = BytesGenerator(fp, mangle_from_=False, policy=policy)
g.flatten(self, unixfrom=unixfrom)
return fp.getvalue()
示例6: msg_as_input
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def msg_as_input(self, msg):
m = message_from_bytes(msg, policy=policy.SMTP)
b = io.BytesIO()
g = BytesGenerator(b)
g.flatten(m)
self.assertEqual(b.getvalue(), msg)
# XXX: spaces are not preserved correctly here yet in the general case.
示例7: test_cte_type_7bit_handles_unknown_8bit
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_cte_type_7bit_handles_unknown_8bit(self):
source = ("Subject: Maintenant je vous présente mon "
"collègue\n\n").encode('utf-8')
expected = ('Subject: Maintenant je vous =?unknown-8bit?q?'
'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii')
msg = message_from_bytes(source)
s = io.BytesIO()
g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit'))
g.flatten(msg)
self.assertEqual(s.getvalue(), expected)
示例8: test_cte_type_7bit_transforms_8bit_cte
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_cte_type_7bit_transforms_8bit_cte(self):
source = textwrap.dedent("""\
From: foo@bar.com
To: Dinsdale
Subject: Nudge nudge, wink, wink
Mime-Version: 1.0
Content-Type: text/plain; charset="latin-1"
Content-Transfer-Encoding: 8bit
oh là là, know what I mean, know what I mean?
""").encode('latin1')
msg = message_from_bytes(source)
expected = textwrap.dedent("""\
From: foo@bar.com
To: Dinsdale
Subject: Nudge nudge, wink, wink
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
oh l=E0 l=E0, know what I mean, know what I mean?
""").encode('ascii')
s = io.BytesIO()
g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit',
linesep='\n'))
g.flatten(msg)
self.assertEqual(s.getvalue(), expected)
示例9: test_byte_message_rfc822_only
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_byte_message_rfc822_only(self):
# Make sure new bytes header parser also passes this.
with openfile('msg_46.txt') as fp:
msgdata = fp.read().encode('ascii')
parser = email.parser.BytesHeaderParser()
msg = parser.parsebytes(msgdata)
out = BytesIO()
gen = email.generator.BytesGenerator(out)
gen.flatten(msg)
self.assertEqual(out.getvalue(), msgdata)
示例10: test_as_bytes_policy
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_as_bytes_policy(self):
msg = self._msgobj('msg_01.txt')
newpolicy = msg.policy.clone(linesep='\r\n')
fullrepr = msg.as_bytes(policy=newpolicy)
s = BytesIO()
g = BytesGenerator(s,policy=newpolicy)
g.flatten(msg)
self.assertEqual(fullrepr, s.getvalue())
# test_headerregistry.TestContentTypeHeader.bad_params
示例11: test_binary_body_with_encode_quopri
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_binary_body_with_encode_quopri(self):
# Issue 14360.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff '
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_quopri)
self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20')
self.assertEqual(msg.get_payload(decode=True), bytesdata)
self.assertEqual(msg['Content-Transfer-Encoding'], 'quoted-printable')
s = BytesIO()
g = BytesGenerator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_bytes(wireform)
self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20')
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
self.assertEqual(msg2['Content-Transfer-Encoding'], 'quoted-printable')
示例12: test_binary_body_with_encode_base64
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_binary_body_with_encode_base64(self):
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_base64)
self.assertEqual(msg.get_payload(), '+vv8/f7/\n')
self.assertEqual(msg.get_payload(decode=True), bytesdata)
s = BytesIO()
g = BytesGenerator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_bytes(wireform)
self.assertEqual(msg.get_payload(), '+vv8/f7/\n')
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
# Test the basic MIMEText class
示例13: test_BytesGenerator_linend
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_BytesGenerator_linend(self):
# Issue 14645.
with openfile('msg_26.txt', newline='\n') as f:
msgtxt = f.read()
msgtxt_nl = msgtxt.replace('\r\n', '\n')
msg = email.message_from_string(msgtxt_nl)
s = BytesIO()
g = email.generator.BytesGenerator(s)
g.flatten(msg, linesep='\r\n')
self.assertEqual(s.getvalue().decode('ascii'), msgtxt)
示例14: test_BytesGenerator_linend_with_non_ascii
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_BytesGenerator_linend_with_non_ascii(self):
# Issue 14645.
with openfile('msg_26.txt', 'rb') as f:
msgtxt = f.read()
msgtxt = msgtxt.replace(b'with attachment', b'fo\xf6')
msgtxt_nl = msgtxt.replace(b'\r\n', b'\n')
msg = email.message_from_bytes(msgtxt_nl)
s = BytesIO()
g = email.generator.BytesGenerator(s)
g.flatten(msg, linesep='\r\n')
self.assertEqual(s.getvalue(), msgtxt)
# Test the iterator/generators
示例15: test_bytes_generator
# 需要导入模块: from email import generator [as 别名]
# 或者: from email.generator import BytesGenerator [as 别名]
def test_bytes_generator(self):
msg = email.message_from_bytes(self.non_latin_bin_msg)
out = BytesIO()
email.generator.BytesGenerator(out).flatten(msg)
self.assertEqual(out.getvalue(), self.non_latin_bin_msg)