本文整理汇总了Python中email.message_from_string方法的典型用法代码示例。如果您正苦于以下问题:Python email.message_from_string方法的具体用法?Python email.message_from_string怎么用?Python email.message_from_string使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email
的用法示例。
在下文中一共展示了email.message_from_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_split_long_continuation
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_split_long_continuation(self):
eq = self.ndiffAssertEqual
msg = email.message_from_string("""\
Subject: bug demonstration
\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
\tmore text
test
""")
sfp = StringIO()
g = Generator(sfp)
g.flatten(msg)
eq(sfp.getvalue(), """\
Subject: bug demonstration
12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
more text
test
""")
示例2: test_boundary_with_leading_space
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_boundary_with_leading_space(self):
eq = self.assertEqual
msg = email.message_from_string('''\
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=" XXXX"
-- XXXX
Content-Type: text/plain
-- XXXX
Content-Type: text/plain
-- XXXX--
''')
self.assertTrue(msg.is_multipart())
eq(msg.get_boundary(), ' XXXX')
eq(len(msg.get_payload()), 2)
示例3: test_message_from_string_with_class
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_message_from_string_with_class(self):
fp = openfile('msg_01.txt')
try:
text = fp.read()
finally:
fp.close()
# Create a subclass
class MyMessage(Message):
pass
msg = email.message_from_string(text, MyMessage)
self.assertIsInstance(msg, MyMessage)
# Try something more complicated
fp = openfile('msg_02.txt')
try:
text = fp.read()
finally:
fp.close()
msg = email.message_from_string(text, MyMessage)
for subpart in msg.walk():
self.assertIsInstance(subpart, MyMessage)
示例4: test_whitespace_continuation
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_whitespace_continuation(self):
eq = self.assertEqual
# This message contains a line after the Subject: header that has only
# whitespace, but it is not empty!
msg = email.message_from_string("""\
From: aperson@dom.ain
To: bperson@dom.ain
Subject: the next line has a space on it
\x20
Date: Mon, 8 Apr 2002 15:09:19 -0400
Message-ID: spam
Here's the message body
""")
eq(msg['subject'], 'the next line has a space on it\n ')
eq(msg['message-id'], 'spam')
eq(msg.get_payload(), "Here's the message body\n")
示例5: test_whitespace_continuation_last_header
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_whitespace_continuation_last_header(self):
eq = self.assertEqual
# Like the previous test, but the subject line is the last
# header.
msg = email.message_from_string("""\
From: aperson@dom.ain
To: bperson@dom.ain
Date: Mon, 8 Apr 2002 15:09:19 -0400
Message-ID: spam
Subject: the next line has a space on it
\x20
Here's the message body
""")
eq(msg['subject'], 'the next line has a space on it\n ')
eq(msg['message-id'], 'spam')
eq(msg.get_payload(), "Here's the message body\n")
示例6: test_CRLFLF_at_end_of_part
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_CRLFLF_at_end_of_part(self):
# issue 5610: feedparser should not eat two chars from body part ending
# with "\r\n\n".
m = (
"From: foo@bar.com\n"
"To: baz\n"
"Mime-Version: 1.0\n"
"Content-Type: multipart/mixed; boundary=BOUNDARY\n"
"\n"
"--BOUNDARY\n"
"Content-Type: text/plain\n"
"\n"
"body ending with CRLF newline\r\n"
"\n"
"--BOUNDARY--\n"
)
msg = email.message_from_string(m)
self.assertTrue(msg.get_payload(0).get_payload().endswith('\r\n'))
示例7: test_rfc2231_unencoded_then_encoded_segments
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_rfc2231_unencoded_then_encoded_segments(self):
eq = self.assertEqual
m = """\
Content-Type: application/x-foo;
\tname*0=\"us-ascii'en-us'My\";
\tname*1*=\" Document\";
\tname*2*=\" For You\"
"""
msg = email.message_from_string(m)
charset, language, s = msg.get_param('name')
eq(charset, 'us-ascii')
eq(language, 'en-us')
eq(s, 'My Document For You')
# Tests to ensure that signed parts of an email are completely preserved, as
# required by RFC1847 section 2.1. Note that these are incomplete, because the
# email package does not currently always preserve the body. See issue 1670765.
示例8: test_get_param
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_get_param(self):
eq = self.assertEqual
msg = email.message_from_string(
"X-Header: foo=one; bar=two; baz=three\n")
eq(msg.get_param('bar', header='x-header'), 'two')
eq(msg.get_param('quuz', header='x-header'), None)
eq(msg.get_param('quuz'), None)
msg = email.message_from_string(
'X-Header: foo; bar="one"; baz=two\n')
eq(msg.get_param('foo', header='x-header'), '')
eq(msg.get_param('bar', header='x-header'), 'one')
eq(msg.get_param('baz', header='x-header'), 'two')
# XXX: We are not RFC-2045 compliant! We cannot parse:
# msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"'
# msg.get_param("weird")
# yet.
示例9: test_binary_body_with_encode_7or8bit
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [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')
示例10: test_binary_body_with_encode_noop
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_binary_body_with_encode_noop(self):
# Issue 16564: This does not produce an RFC valid message, since to be
# valid it should have a CTE of binary. But the below works, and is
# documented as working this way.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop)
self.assertEqual(msg.get_payload(), bytesdata)
self.assertEqual(msg.get_payload(decode=True), bytesdata)
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)
# Test the basic MIMEText class
示例11: test__all__
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test__all__(self):
module = __import__('email')
# Can't use sorted() here due to Python 2.3 compatibility
all = module.__all__[:]
all.sort()
self.assertEqual(all, [
# Old names
'Charset', 'Encoders', 'Errors', 'Generator',
'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
'MIMENonMultipart', 'MIMEText', 'Message',
'Parser', 'Utils', 'base64MIME',
# new names
'base64mime', 'charset', 'encoders', 'errors', 'generator',
'header', 'iterators', 'message', 'message_from_file',
'message_from_string', 'mime', 'parser',
'quopriMIME', 'quoprimime', 'utils',
])
示例12: test_copy
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def test_copy():
email_str = """
From: abcd@gmail.com
To: hey@example.org
Subject: subject
Body
"""
msg = email.message_from_string(email_str)
msg2 = copy(msg)
assert msg.as_bytes() == msg2.as_bytes()
示例13: _http_request_requests
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def _http_request_requests(self, url, headers, data, params):
kwargs = {
'verify': self.verify_cert,
}
if url is not None:
kwargs['url'] = url
if headers is not None:
kwargs['headers'] = headers
if data is not None:
kwargs['data'] = data
if params is not None:
kwargs['params'] = params
if self.timeout is not None:
kwargs['timeout'] = self.timeout
try:
if data is None:
r = requests.get(**kwargs)
else:
r = requests.post(**kwargs)
except requests.exceptions.RequestException as e:
raise PanHttpError('RequestException: ' + str(e))
self.code = r.status_code
self.reason = r.reason
x = ['%s: %s' % (k, v) for k, v in r.headers.items()]
try:
self.headers = email.message_from_string('\n'.join(x))
except (TypeError, email.errors.MessageError) as e:
raise PanHttpError('email.message_from_string() %s' % e)
self.encoding = self.headers.get_content_charset('utf8')
self.content_type = self.headers.get_content_type()
self.content = r.content
self.text = r.text
# allow non-2XX error codes
# see http://bugs.python.org/issue18543 for why we can't just
# install a new HTTPErrorProcessor()
示例14: get_mime_message
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def get_mime_message(
self,
msg_id,
user_id='me'):
"""Get a Message and use it to create a MIME Message.
:param msg_id: The ID of the Message required.
:param user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
:return: A MIME Message, consisting of data from Message.
:rtype: :class:`email.message.Message`
"""
self.connect()
ret = None
try:
message = self.client.users().messages().get(
userId=user_id,
id=msg_id,
format='raw'
).execute()
except (socks.HTTPError, errors.HttpError) as error:
logger.error('An error occurred: %s', error)
else:
logger.info('Message snippet: %s', message.get('snippet', ''))
msg_str = urlsafe_b64decode(message.get('raw', '').encode('ASCII'))
ret = message_from_string(msg_str)
return ret
示例15: run
# 需要导入模块: import email [as 别名]
# 或者: from email import message_from_string [as 别名]
def run(self, params={}):
try:
eml_file = base64.b64decode(params.get(Input.EML_FILE)).decode('utf-8')
except Exception as ex:
self.logger.debug(ex)
self.logger.debug("Failed to parse message as UTF-8, attempting to detwingle first before retrying parse")
eml_file = UnicodeDammit.detwingle(base64.b64decode(params.get(Input.EML_FILE))).decode('utf-8', errors='ignore')
msg = email.message_from_string(eml_file)
result = format_output.format_result(self.logger, msg)
return {Output.RESULT: result}