Python quopri.decodestring() Examples

The following are 30 code examples of quopri.decodestring(). 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 quopri , or try the search function .
Example #1
Source File: mailgun.py    From fluentmail with MIT License 6 votes vote down vote up
def _build_attachments(self, message):
        attachments = []

        for attachment in message.attachments:
            file_name = attachment.get_filename()
            encoding = attachment.get('Content-Transfer-Encoding', None)

            if encoding == 'base64':
                decode = not attachment.get_content_maintype() in ['audio', 'image', 'text']
                content = b64decode(attachment.get_payload(decode=decode))
            elif encoding == 'quoted-printable':
                content = decode_quopri(attachment.get_payload())
            else:
                content = attachment.get_payload()

            content_type = attachment.get_content_type()
            attachments.append(('attachment', (file_name, content, content_type)))

        return attachments 
Example #2
Source File: eml_parser.py    From Python-Digital-Forensics-Cookbook with MIT License 6 votes vote down vote up
def process_payload(payload):
    print(payload.get_content_type() + "\n" + "=" * len(
        payload.get_content_type()))
    body = quopri.decodestring(payload.get_payload())
    if payload.get_charset():
        body = body.decode(payload.get_charset())
    else:
        try:
            body = body.decode()
        except UnicodeDecodeError:
            body = body.decode('cp1252')

    if payload.get_content_type() == "text/html":
        outfile = os.path.basename(args.EML_FILE.name) + ".html"
        open(outfile, 'w').write(body)
    elif payload.get_content_type().startswith('application'):
        outfile = open(payload.get_filename(), 'wb')
        body = base64.b64decode(payload.get_payload())
        outfile.write(body)
        outfile.close()
        print("Exported: {}\n".format(outfile.name))
    else:
        print(body) 
Example #3
Source File: parser.py    From zmail with MIT License 6 votes vote down vote up
def _decode_one_part_body(lines: List[bytes], transfer_encoding: str, charsets: List[str], _need_decode=True):
    """Decode transfer-encoding then decode raw value to string."""
    if transfer_encoding == 'quoted-printable':
        decoded_bytes = decodestring(b'\r\n'.join(lines))
        if _need_decode:
            return recursive_decode(decoded_bytes, charsets)
        else:
            return b'\r\n'.join(lines)
    elif transfer_encoding == 'base64':
        decoded_bytes = b64decode(b''.join(lines))
        if _need_decode:
            return recursive_decode(decoded_bytes, charsets)
        else:
            return decoded_bytes
    elif transfer_encoding in ('binary', '8bit', '7bit'):
        if _need_decode:
            return recursive_decode(b'\r\n'.join(lines), charsets)
        else:
            return b'\r\n'.join(lines)
    else:
        raise ParseError('Invalid transfer-encoding {}'.format(transfer_encoding)) 
Example #4
Source File: utils.py    From modoboa-webmail with MIT License 6 votes vote down vote up
def decode_payload(encoding, payload):
    """Decode the payload according to the given encoding

    Supported encodings: base64, quoted-printable.

    :param encoding: the encoding's name
    :param payload: the value to decode
    :return: a string
    """
    encoding = encoding.lower()
    if encoding == "base64":
        import base64
        return base64.b64decode(payload)
    elif encoding == "quoted-printable":
        import quopri
        return quopri.decodestring(payload)
    return payload 
Example #5
Source File: test_quopri.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_decodestring(self):
        for p, e in self.STRINGS:
            self.assertEqual(quopri.decodestring(e), p) 
Example #6
Source File: test_quopri.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_decodestring_double_equals(self):
        # Issue 21511 - Ensure that byte string is compared to byte string
        # instead of int byte value
        decoded_value, encoded_value = (b"123=four", b"123==four")
        self.assertEqual(quopri.decodestring(encoded_value), decoded_value) 
Example #7
Source File: test_quopri.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_decode_header(self):
        for p, e in self.HSTRINGS:
            self.assertTrue(quopri.decodestring(e, header=True) == p) 
Example #8
Source File: test_quopri.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_idempotent_string(self):
        for p, e in self.STRINGS:
            self.assertTrue(quopri.decodestring(quopri.encodestring(e)) == e) 
Example #9
Source File: test_quopri.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_embedded_ws(self):
        for p, e in self.ESTRINGS:
            self.assertTrue(quopri.encodestring(p, quotetabs=True) == e)
            self.assertTrue(quopri.decodestring(e) == p) 
Example #10
Source File: test_quopri.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_idempotent_string(self):
        for p, e in self.STRINGS:
            self.assertEqual(quopri.decodestring(quopri.encodestring(e)), e) 
Example #11
Source File: test_quopri.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_embedded_ws(self):
        for p, e in self.ESTRINGS:
            self.assertEqual(quopri.encodestring(p, quotetabs=True), e)
            self.assertEqual(quopri.decodestring(e), p) 
Example #12
Source File: test_quopri.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_decode_header(self):
        for p, e in self.HSTRINGS:
            self.assertEqual(quopri.decodestring(e, header=True), p) 
Example #13
Source File: eml.py    From fame_modules with GNU General Public License v3.0 5 votes vote down vote up
def extract_urls(self, mail):
        regex_url = r"\w+:(\/\/)[^\s>\"\'\"]+"
        reg = re.compile(regex_url)
        content = quopri.decodestring(mail.as_string())
        for match in reg.finditer(content):
            self.add_ioc(match.group(0)) 
Example #14
Source File: test_quopri.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_decodestring(self):
        for p, e in self.STRINGS:
            self.assert_(quopri.decodestring(e) == p) 
Example #15
Source File: test_quopri.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_idempotent_string(self):
        for p, e in self.STRINGS:
            self.assert_(quopri.decodestring(quopri.encodestring(e)) == e) 
Example #16
Source File: test_quopri.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_embedded_ws(self):
        for p, e in self.ESTRINGS:
            self.assert_(quopri.encodestring(p, quotetabs=True) == e)
            self.assert_(quopri.decodestring(e) == p) 
Example #17
Source File: test_quopri.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_decode_header(self):
        for p, e in self.HSTRINGS:
            self.assert_(quopri.decodestring(e, header=True) == p) 
Example #18
Source File: leap_attachment_store.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def _try_decode(self, raw, encoding):
        encoding = encoding.lower()
        if encoding == 'base64':
            data = base64.decodestring(raw)
        elif encoding == 'quoted-printable':
            data = quopri.decodestring(raw)
        else:
            data = str(raw)

        return bytearray(data) 
Example #19
Source File: plugin_quopri.py    From deen with Apache License 2.0 5 votes vote down vote up
def unprocess(self, data):
        super(DeenPluginQuopri, self).unprocess(data)
        try:
            data = quopri.decodestring(data)
        except Exception as e:
            self.error = e
            self.log.error(self.error)
            self.log.debug(self.error, exc_info=True)
        return data 
Example #20
Source File: test_quopri.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_decodestring(self):
        for p, e in self.STRINGS:
            self.assertTrue(quopri.decodestring(e) == p) 
Example #21
Source File: test_quopri.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_idempotent_string(self):
        for p, e in self.STRINGS:
            self.assertTrue(quopri.decodestring(quopri.encodestring(e)) == e) 
Example #22
Source File: test_quopri.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_embedded_ws(self):
        for p, e in self.ESTRINGS:
            self.assertTrue(quopri.encodestring(p, quotetabs=True) == e)
            self.assertTrue(quopri.decodestring(e) == p) 
Example #23
Source File: test_quopri.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_decode_header(self):
        for p, e in self.HSTRINGS:
            self.assertTrue(quopri.decodestring(e, header=True) == p) 
Example #24
Source File: tests.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def check_quopri(msg, data):
    assert msg._payload.encode() != data, "Payload has not been transformed"
    assert quopri.decodestring(msg._payload) == data, "Payload was not encoded correctly"

    assert INBOXEN_ENCODING_ERROR_HEADER_NAME not in msg.keys(), "Unexpected error header" 
Example #25
Source File: ical.py    From ls.joyous with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __str__(self):
        retval = super().__str__()
        param = self.params.get('ENCODING', "").upper()
        if param == 'QUOTED-PRINTABLE':
            retval = quopri.decodestring(retval).decode(self.encoding, 'ignore')
        elif param == 'BASE64':
            retval = base64.b64decode(retval).decode(self.encoding, 'ignore')
        return retval 
Example #26
Source File: test_quopri.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_decodestring(self):
        for p, e in self.STRINGS:
            self.assertTrue(quopri.decodestring(e) == p) 
Example #27
Source File: test_quopri.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_idempotent_string(self):
        for p, e in self.STRINGS:
            self.assertTrue(quopri.decodestring(quopri.encodestring(e)) == e) 
Example #28
Source File: test_quopri.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_embedded_ws(self):
        for p, e in self.ESTRINGS:
            self.assertTrue(quopri.encodestring(p, quotetabs=True) == e)
            self.assertTrue(quopri.decodestring(e) == p) 
Example #29
Source File: test_quopri.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_decode_header(self):
        for p, e in self.HSTRINGS:
            self.assertTrue(quopri.decodestring(e, header=True) == p) 
Example #30
Source File: email_message.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_decoded_payload(self):
        """
        Similar to get_payload(decode=True), but:

        * decoding failures are logged as warnings (instead of being
          completely silent);
        * if the message is a multipart one, the payload of its first
          part is extracted (with `.get_decoded_payload()`, recursively)
          and returned instead of `None`.
        """
        # copied from Py2.7's email.message.Message.get_payload() and adjusted
        payload = self._payload
        if self.is_multipart():
            first_part = payload[0]
            assert isinstance(first_part, self.__class__)
            return first_part.get_decoded_payload()
        cte = self.get('content-transfer-encoding', '').lower()
        if cte == 'quoted-printable':
            payload = quopri.decodestring(payload)
        elif cte == 'base64':
            if payload:
                try:
                    payload = base64.decodestring(payload)
                except binascii.Error:
                    LOGGER.warning('Could not decode the payload using base64'
                                   ' => not decoding')
                    LOGGER.debug('The payload: %r', payload)
        elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
            sfp = StringIO()
            try:
                uu.decode(StringIO(payload + '\n'), sfp, quiet=True)
                payload = sfp.getvalue()
            except uu.Error:
                LOGGER.warning('Could not decode the payload using %s'
                               ' => not decoding', cte)
                LOGGER.debug('The payload: %r', payload)
        elif cte not in ('', '7bit', '8bit', 'binary'):
            LOGGER.warning('Unsupported content-transfer-encoding: %s'
                           ' => not decoding', cte)
            LOGGER.debug('The payload: %r', payload)
        return payload