Python email.parser.HeaderParser() Examples

The following are 23 code examples of email.parser.HeaderParser(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module email.parser , or try the search function .
Example #1
Source File: utils.py    From GTDWeb with GNU General Public License v2.0 7 votes vote down vote up
def parse_docstring(docstring):
    """
    Parse out the parts of a docstring.  Return (title, body, metadata).
    """
    docstring = trim_docstring(docstring)
    parts = re.split(r'\n{2,}', docstring)
    title = parts[0]
    if len(parts) == 1:
        body = ''
        metadata = {}
    else:
        parser = HeaderParser()
        try:
            metadata = parser.parsestr(parts[-1])
        except HeaderParseError:
            metadata = {}
            body = "\n\n".join(parts[1:])
        else:
            metadata = dict(metadata.items())
            if metadata:
                body = "\n\n".join(parts[1:-1])
            else:
                body = "\n\n".join(parts[1:])
    return title, body, metadata 
Example #2
Source File: as2lib.py    From pyas2 with GNU General Public License v2.0 6 votes vote down vote up
def run_post_send(message):
    """ Execute command after successful send, can be used to notify successful sends """

    command = message.partner.cmd_send
    if command:
        models.Log.objects.create(message=message, status='S', text=_(u'Execute command post successful send'))
        # Create command template and replace variables in the command
        command = Template(command)
        variables = {
            'filename': message.payload.name,
            'sender': message.organization.as2_name,
            'recevier': message.partner.as2_name,
            'messageid': message.message_id
        }
        variables.update(dict(HeaderParser().parsestr(message.headers).items()))

        # Execute the command
        os.system(command.safe_substitute(variables)) 
Example #3
Source File: as2lib.py    From pyas2 with GNU General Public License v2.0 6 votes vote down vote up
def run_post_receive(message, full_filename):
    """ Execute command after successful receive, can be used to call the edi program for further processing"""

    command = message.partner.cmd_receive
    if command:
        models.Log.objects.create(message=message, status='S', text=_(u'Execute command post successful receive'))
        # Create command template and replace variables in the command
        command = Template(command)
        variables = {
            'filename': message.payload.name,
            'fullfilename': full_filename,
            'sender': message.organization.as2_name,
            'recevier': message.partner.as2_name,
            'messageid': message.message_id
        }
        variables.update(dict(HeaderParser().parsestr(message.headers).items()))

        # Execute the command
        os.system(command.safe_substitute(variables)) 
Example #4
Source File: crispin.py    From sync-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
def find_by_header(self, header_name, header_value):
        """Find all uids in the selected folder with the given header value."""
        all_uids = self.all_uids()
        # It would be nice to just search by header too, but some backends
        # don't support that, at least not if you want to search by X-INBOX-ID
        # header. So fetch the header for each draft and see if we
        # can find one that matches.
        # TODO(emfree): are there other ways we can narrow the result set a
        # priori (by subject or date, etc.)
        matching_draft_headers = self.fetch_headers(all_uids)
        results = []
        for uid, response in matching_draft_headers.iteritems():
            headers = response['BODY[HEADER]']
            parser = HeaderParser()
            header = parser.parsestr(headers).get(header_name)
            if header == header_value:
                results.append(uid)

        return results 
Example #5
Source File: views.py    From pyas2 with GNU General Public License v2.0 6 votes vote down vote up
def get(self, request, pk, *args, **kwargs):
        try:
            mdn = models.MDN.objects.get(message_id=pk)
            if request.GET['action'] == 'downl':
                response = HttpResponse(content_type='multipart/report')
                disposition_type = 'attachment'
                response['Content-Disposition'] = disposition_type + '; filename=' + pk + '.mdn'
                response.write(as2utils.readdata(mdn.file))
                return response
            elif request.GET['action'] == 'this':
                file_obj = dict()
                file_obj['name'] = pk + '.mdn'
                file_obj['id'] = pk
                file_obj['content'] = as2utils.readdata(mdn.file, charset='utf-8', errors='ignore')
                file_obj['direction'] = mdn.get_status_display()
                file_obj['type'] = 'AS2 MDN'
                file_obj['headers'] = dict(HeaderParser().parsestr(mdn.headers or '').items())
                return render(request, self.template_name, {'file_obj': file_obj})
        except Exception:
            return render(request, self.template_name, {'error_content': _(u'No such file.')}) 
Example #6
Source File: models.py    From django-pyas2 with GNU General Public License v3.0 6 votes vote down vote up
def send_async_mdn(self):
        """ Send the asynchronous MDN to the partner"""

        # convert the mdn headers to dictionary
        headers = HeaderParser().parsestr(self.headers.read().decode())

        # Send the mdn to the partner
        try:
            response = requests.post(
                self.return_url, headers=dict(headers.items()), data=self.payload.read()
            )
            response.raise_for_status()
        except requests.exceptions.RequestException:
            return

        # Update the status of the MDN
        self.status = "S"
        self.save() 
Example #7
Source File: utils.py    From openhgsenti with Apache License 2.0 6 votes vote down vote up
def parse_docstring(docstring):
    """
    Parse out the parts of a docstring.  Return (title, body, metadata).
    """
    docstring = trim_docstring(docstring)
    parts = re.split(r'\n{2,}', docstring)
    title = parts[0]
    if len(parts) == 1:
        body = ''
        metadata = {}
    else:
        parser = HeaderParser()
        try:
            metadata = parser.parsestr(parts[-1])
        except HeaderParseError:
            metadata = {}
            body = "\n\n".join(parts[1:])
        else:
            metadata = dict(metadata.items())
            if metadata:
                body = "\n\n".join(parts[1:-1])
            else:
                body = "\n\n".join(parts[1:])
    return title, body, metadata 
Example #8
Source File: test_email_renamed.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertIsInstance(msg.get_payload(), str) 
Example #9
Source File: test_email_renamed.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertTrue(isinstance(msg.get_payload(), str)) 
Example #10
Source File: test_email_renamed.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertTrue(isinstance(msg.get_payload(), str)) 
Example #11
Source File: test_email_renamed.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertTrue(isinstance(msg.get_payload(), str)) 
Example #12
Source File: test_email_renamed.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertTrue(isinstance(msg.get_payload(), str)) 
Example #13
Source File: test_email_renamed.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.failIf(msg.is_multipart())
        self.failUnless(isinstance(msg.get_payload(), str)) 
Example #14
Source File: email_headers.py    From fame_modules with GNU General Public License v3.0 5 votes vote down vote up
def each(self, target):
        self.results = {}

        header_raw = open(target, 'r').read()
        header = HeaderParser()
        parsed_headers = header.parsestr(header_raw)

        # Get Useful Headers
        self.results['From'] = decode_mime_words(parsed_headers['From'])
        self.results['ReturnPath'] = decode_mime_words(parsed_headers['Return-Path'])
        self.results['ReplyTo'] = decode_mime_words(parsed_headers['Reply-To'])
        self.results['To'] = decode_mime_words(parsed_headers['To'])
        self.results['Subject'] = decode_mime_words(parsed_headers['Subject'])
        self.results['Date'] = parsed_headers['Date']
        self.results['Cc'] = decode_mime_words(parsed_headers['Cc'])

        # Parse Received and Authentication Headers
        self.results['Received'] = self.parse_received(parsed_headers.get_all('Received'))
        self.results['DKIM'] = self.parse_dkim(parsed_headers.items())
        self.results['SPF'] = self.parse_spf(parsed_headers.items())
        self.results['DMARC'] = self.parse_dmarc(parsed_headers.items())

        self.results['headers'] = parsed_headers.items()
        self.results['highlight'] = self.highlight

        return True 
Example #15
Source File: test_email_renamed.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertIsInstance(msg.get_payload(), str) 
Example #16
Source File: test_email.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        with openfile('msg_02.txt') as fp:
            msg = HeaderParser().parse(fp)
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertIsInstance(msg.get_payload(), str) 
Example #17
Source File: test_email.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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) 
Example #18
Source File: tests.py    From pyas2 with GNU General Public License v2.0 5 votes vote down vote up
def setUpTestData(cls):
        # Every test needs a client.
        cls.client = Client()
        cls.header_parser = HeaderParser()

        # Load the client and server certificates
        cls.server_key = models.PrivateCertificate.objects.create(
            certificate=os.path.join(TEST_DIR, 'as2server.pem'),
            certificate_passphrase='password'
        )
        cls.si_public_key = models.PublicCertificate.objects.create(
            certificate=os.path.join(TEST_DIR, 'si_public_key.crt'),
            ca_cert=os.path.join(TEST_DIR, 'si_public_key.ca'),
            verify_cert=False
        )

        # Setup the server organization and partner
        cls.organization = models.Organization.objects.create(
            name='Server Organization',
            as2_name='as2server',
            encryption_key=cls.server_key,
            signature_key=cls.server_key
        )

        cls.partner = models.Partner.objects.create(
            name='Sterling B2B Integrator',
            as2_name='SIAS2PRD',
            target_url='http://localhost:8080/pyas2/as2receive',
            compress=False,
            mdn=False,
            signature_key=cls.si_public_key,
            encryption_key=cls.si_public_key
        )

        # Initialise the payload i.e. the file to be transmitted
        cls.payload = models.Payload.objects.create(
            name='testmessage.edi',
            file=os.path.join(TEST_DIR, 'testmessage.edi'),
            content_type='application/edi-consent'
        ) 
Example #19
Source File: sendasyncmdn.py    From pyas2 with GNU General Public License v2.0 5 votes vote down vote up
def handle(self, *args, **options):
        # First part of script sends asynchronous MDNs for inbound messages received from partners
        # Fetch all the pending asynchronous MDN objects
        pyas2init.logger.info(_(u'Sending all pending asynchronous MDNs'))
        in_pending_mdns = models.MDN.objects.filter(status='P')  # , timestamp__gt=time_threshold) --> why do this?

        for pending_mdn in in_pending_mdns:
            # Parse the MDN headers from text
            header_parser = HeaderParser()
            mdn_headers = header_parser.parsestr(pending_mdn.headers)
            try:
                # Set http basic auth if enabled in the partner profile
                auth = None
                if pending_mdn.omessage.partner and pending_mdn.omessage.partner.http_auth:
                    auth = (pending_mdn.omessage.partner.http_auth_user, pending_mdn.omessage.partner.http_auth_pass)

                # Set the ca cert if given in the partner profile
                verify = True
                if pending_mdn.omessage.partner.https_ca_cert:
                    verify = pending_mdn.omessage.partner.https_ca_cert.path

                # Post the MDN message to the url provided on the original as2 message
                with open(pending_mdn.file, 'rb') as payload:
                    requests.post(pending_mdn.return_url,
                                  auth=auth,
                                  verify=verify,
                                  headers=dict(mdn_headers.items()),
                                  data=payload)
                pending_mdn.status = 'S'
                models.Log.objects.create(message=pending_mdn.omessage,
                                          status='S',
                                          text=_(u'Successfully sent asynchronous mdn to partner'))
            except Exception, e:
                models.Log.objects.create(message=pending_mdn.omessage,
                                          status='E',
                                          text=_(
                                              u'Failed to send asynchronous mdn to partner, error is {0:s}'.format(e)))
            finally: 
Example #20
Source File: views.py    From pyas2 with GNU General Public License v2.0 5 votes vote down vote up
def get(self, request, pk, *args, **kwargs):
        try:
            message = models.Message.objects.get(message_id=pk)
            payload = message.payload
            if request.GET['action'] == 'downl':
                # Returns the payload contents as an attachment, thus enabling users to download the data
                response = HttpResponse(content_type=payload.content_type)
                disposition_type = 'attachment'
                response['Content-Disposition'] = disposition_type + '; filename=' + payload.name
                response.write(as2utils.readdata(payload.file))
                return response
            elif request.GET['action'] == 'this':
                # Displays the payload contents, Formatting is applied based on the content type of the message.
                file_obj = dict()
                file_obj['name'] = payload.name
                file_obj['id'] = pk
                file_obj['content'] = as2utils.readdata(payload.file, charset='utf-8', errors='ignore')
                if payload.content_type == 'application/EDI-X12':
                    file_obj['content'] = viewlib.indent_x12(file_obj['content'])
                elif payload.content_type == 'application/EDIFACT':
                    file_obj['content'] = viewlib.indent_edifact(file_obj['content'])
                elif payload.content_type == 'application/XML':
                    file_obj['content'] = viewlib.indent_xml(file_obj['content'])
                file_obj['direction'] = message.get_direction_display()
                file_obj['type'] = 'AS2 MESSAGE'
                file_obj['headers'] = dict(HeaderParser().parsestr(message.headers or '').items())
                return render(request, self.template_name, {'file_obj': file_obj})
        except Exception:
            return render(request, self.template_name, {'error_content': _(u'No such file.')}) 
Example #21
Source File: test_email_renamed.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertIsInstance(msg.get_payload(), str) 
Example #22
Source File: test_email_renamed.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_header_parser(self):
        eq = self.assertEqual
        # Parse only the headers of a complex multipart MIME document
        fp = openfile('msg_02.txt')
        try:
            msg = HeaderParser().parse(fp)
        finally:
            fp.close()
        eq(msg['from'], 'ppp-request@zzz.org')
        eq(msg['to'], 'ppp@zzz.org')
        eq(msg.get_content_type(), 'multipart/mixed')
        self.assertFalse(msg.is_multipart())
        self.assertTrue(isinstance(msg.get_payload(), str)) 
Example #23
Source File: test_basic.py    From django-pyas2 with GNU General Public License v3.0 4 votes vote down vote up
def setUpTestData(cls):
        # Every test needs a client.
        cls.client = Client()
        cls.header_parser = HeaderParser()

        # Load the client and server certificates
        with open(os.path.join(TEST_DIR, "server_private.pem"), "rb") as fp:
            cls.server_key = PrivateKey.objects.create(key=fp.read(), key_pass="test")

        with open(os.path.join(TEST_DIR, "server_public.pem"), "rb") as fp:
            cls.server_crt = PublicCertificate.objects.create(certificate=fp.read())

        with open(os.path.join(TEST_DIR, "client_private.pem"), "rb") as fp:
            cls.client_key = PrivateKey.objects.create(key=fp.read(), key_pass="test")

        with open(os.path.join(TEST_DIR, "client_public.pem"), "rb") as fp:
            cls.client_crt = PublicCertificate.objects.create(certificate=fp.read())

        # Setup the server organization and partner
        Organization.objects.create(
            name="AS2 Server",
            as2_name="as2server",
            encryption_key=cls.server_key,
            signature_key=cls.server_key,
        )
        Partner.objects.create(
            name="AS2 Client",
            as2_name="as2client",
            target_url="http://localhost:8080/pyas2/as2receive",
            compress=False,
            mdn=False,
            signature_cert=cls.client_crt,
            encryption_cert=cls.client_crt,
        )

        # Setup the client organization and partner
        cls.organization = Organization.objects.create(
            name="AS2 Client",
            as2_name="as2client",
            encryption_key=cls.client_key,
            signature_key=cls.client_key,
        )

        # Initialise the payload i.e. the file to be transmitted
        with open(os.path.join(TEST_DIR, "testmessage.edi"), "rb") as fp:
            cls.payload = fp.read()