我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用email.Parser()。
def test_boundary_without_trailing_newline(self): m = Parser().parsestr("""\ Content-Type: multipart/mixed; boundary="===============0012394164==" MIME-Version: 1.0 --===============0012394164== Content-Type: image/file1.jpg MIME-Version: 1.0 Content-Transfer-Encoding: base64 YXNkZg== --===============0012394164==--""") self.assertEquals(m.get_payload(0).get_payload(), 'YXNkZg==') # Test some badly formatted messages
def test_boundary_without_trailing_newline(self): m = Parser().parsestr("""\ Content-Type: multipart/mixed; boundary="===============0012394164==" MIME-Version: 1.0 --===============0012394164== Content-Type: image/file1.jpg MIME-Version: 1.0 Content-Transfer-Encoding: base64 YXNkZg== --===============0012394164==--""") self.assertEqual(m.get_payload(0).get_payload(), 'YXNkZg==') # Test some badly formatted messages
def test__all__(self): module = __import__('email') 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_crlf_separation(self): eq = self.assertEqual fp = openfile('msg_26.txt', mode='rb') try: msg = Parser().parse(fp) finally: fp.close() eq(len(msg.get_payload()), 2) part1 = msg.get_payload(0) eq(part1.get_content_type(), 'text/plain') eq(part1.get_payload(), 'Simple email with attachment.\r\n\r\n') part2 = msg.get_payload(1) eq(part2.get_content_type(), 'application/riscos')
def test_message_rfc822_only(self): # Issue 7970: message/rfc822 not in multipart parsed by # HeaderParser caused an exception when flattened. fp = openfile(findfile('msg_46.txt')) msgdata = fp.read() parser = email.Parser.HeaderParser() msg = parser.parsestr(msgdata) out = StringIO() gen = email.Generator.Generator(out, True, 0) gen.flatten(msg, False) self.assertEqual(out.getvalue(), msgdata)
def test_partial_falls_inside_message_delivery_status(self): eq = self.ndiffAssertEqual # The Parser interface provides chunks of data to FeedParser in 8192 # byte gulps. SF bug #1076485 found one of those chunks inside # message/delivery-status header block, which triggered an # unreadline() of NeedMoreData. msg = self._msgobj('msg_43.txt') sfp = StringIO() Iterators._structure(msg, sfp) eq(sfp.getvalue(), """\ multipart/report text/plain message/delivery-status text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/plain text/rfc822-headers """) # Test the iterator/generators