我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用email.parser()。
def test_mime_attachments_in_constructor(self): eq = self.assertEqual text1 = MIMEText('') text2 = MIMEText('') msg = MIMEMultipart(_subparts=(text1, text2)) eq(len(msg.get_payload()), 2) eq(msg.get_payload(0), text1) eq(msg.get_payload(1), text2) # A general test of parser->model->generator idempotency. IOW, read a message # in, parse it into a message object tree, then without touching the tree, # regenerate the plain text. The original text and the transformed text # should be identical. Note: that we ignore the Unix-From since that may # contain a changed date.
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', ])
def test_message_rfc822_only(self): # Issue 7970: message/rfc822 not in multipart parsed by # HeaderParser caused an exception when flattened. with openfile(findfile('msg_46.txt')) as fp: msgdata = fp.read() parser = HeaderParser() msg = parser.parsestr(msgdata) out = StringIO() gen = Generator(out, True, 0) gen.flatten(msg, False) self.assertEqual(out.getvalue(), msgdata)
def test_strip_line_feed_and_carriage_return_in_headers(self): eq = self.assertEqual # For [ 1002475 ] email message parser doesn't handle \r\n correctly value1 = 'text' value2 = 'more text' m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % ( value1, value2) msg = email.message_from_string(m) eq(msg.get('Header'), value1) eq(msg.get('Next-Header'), value2)
def test_default_multipart_constructor(self): msg = MIMEMultipart() self.assertTrue(msg.is_multipart()) # A general test of parser->model->generator idempotency. IOW, read a message # in, parse it into a message object tree, then without touching the tree, # regenerate the plain text. The original text and the transformed text # should be identical. Note: that we ignore the Unix-From since that may # contain a changed date.
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, [ 'base64mime', 'charset', 'encoders', 'errors', 'generator', 'header', 'iterators', 'message', 'message_from_binary_file', 'message_from_bytes', 'message_from_file', 'message_from_string', 'mime', 'parser', 'quoprimime', 'utils', ])
def test_message_from_binary_file(self): fn = 'test.msg' self.addCleanup(unlink, fn) with open(fn, 'wb') as testfile: testfile.write(self.non_latin_bin_msg) with open(fn, 'rb') as testfile: m = email.parser.BytesParser().parse(testfile) self.assertEqual(str(m), self.non_latin_bin_msg_as7bit)
def test_unix_mbox(self): ### should be better! import email.parser fname = self.createMessage("cur", True) n = 0 fid = open(fname) for msg in mailbox.PortableUnixMailbox(fid, email.parser.Parser().parse): n += 1 self.assertEqual(msg["subject"], "Simple Test") self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE)) fid.close() self.assertEqual(n, 1) ## End: classes from the original module (for backward compatibility).
def test_parser(self): p = email.parser.Parser(policy=self.MyPolicy) with self.assertRaisesRegex(Exception, "^test$"): p.parsestr('Subject: test\n\n')
def test_bytes_parser(self): p = email.parser.BytesParser(policy=self.MyPolicy) with self.assertRaisesRegex(Exception, "^test$"): p.parsebytes(b'Subject: test\n\n') # Now that we've established that all the parse methods get the # policy in to feedparser, we can use message_from_string for # the rest of the propagation tests.
def test_message_rfc822_only(self): # Issue 7970: message/rfc822 not in multipart parsed by # HeaderParser caused an exception when flattened. with openfile('msg_46.txt') as fp: msgdata = fp.read() parser = HeaderParser() msg = parser.parsestr(msgdata) out = StringIO() gen = Generator(out, True, 0) gen.flatten(msg, False) self.assertEqual(out.getvalue(), msgdata)
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)
def grab_headers(string): global msg_id ret_ar = {} # Pull the headers using the email library parser = email.parser.HeaderParser() headers = parser.parsestr(string) # Needed a unique key for searching for a specific message # I think you could also leverage this for message threads msg_id = re.sub('[<>]', '', headers['Message-ID']) for h in headers.items(): ret_ar[h[0]] = h[1] return ret_ar # Do some basic sanity on the message contents