本文整理汇总了Python中quopri.decodestring方法的典型用法代码示例。如果您正苦于以下问题:Python quopri.decodestring方法的具体用法?Python quopri.decodestring怎么用?Python quopri.decodestring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类quopri
的用法示例。
在下文中一共展示了quopri.decodestring方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_payload
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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)
示例2: _decode_one_part_body
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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))
示例3: decode_payload
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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
示例4: _build_attachments
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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
示例5: test_decodestring
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def test_decodestring(self):
for p, e in self.STRINGS:
self.assertTrue(quopri.decodestring(e) == p)
示例6: test_idempotent_string
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def test_idempotent_string(self):
for p, e in self.STRINGS:
self.assertTrue(quopri.decodestring(quopri.encodestring(e)) == e)
示例7: test_embedded_ws
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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)
示例8: test_decode_header
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def test_decode_header(self):
for p, e in self.HSTRINGS:
self.assertTrue(quopri.decodestring(e, header=True) == p)
示例9: decode
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def decode(self, line):
line = self._consumed_lines + line # add potentially stored previous lines
self._consumed_lines = ''
m = QuotedPrintableDecoder.quoted.match(line)
if m:
line = line[:m.start(1)] + line[m.end(1):] # remove the matched group 1 from line
decoded_line = quopri.decodestring(line).decode('UTF-8')
# Escape newlines, but preserve the last one (which must be '\n', since we read the file in universal newliens mode)
decoded_line = decoded_line[:-1].replace('\r\n', '\\n')
decoded_line = decoded_line.replace('\n', '\\n')
return decoded_line + '\n'
return line
示例10: decode_transfer_encoding
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def decode_transfer_encoding(encoding, body):
if encoding == 'base64':
return _base64_decode(body)
elif encoding == 'quoted-printable':
return quopri.decodestring(body)
else:
return body
示例11: get_decoded_payload
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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
示例12: arion_parse
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def arion_parse(self, payload):
"""Non-multipart but with the plaintext in HTML comment"""
s = payload[:payload.index("-->")]
lines = s.splitlines()
# Fault tolerance, it changes around a bit
try:
amount = int(re.sub(r"[^0-9]", "", lines[9].split(": ")[1]))
raw = quopri.decodestring(lines[11]).split(": ")[1]
except IndexError:
raise ValueError("2 Arion Bankmail without plaintext encoutered")
# Not sure if nessesary or formed user input from user "Numi"
skyring = raw.strip().decode("tis-620")
return skyring, amount, "arion"
示例13: test_decodestring
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def test_decodestring(self):
for p, e in self.STRINGS:
self.assertEqual(quopri.decodestring(e), p)
示例14: test_decodestring_double_equals
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
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)
示例15: test_idempotent_string
# 需要导入模块: import quopri [as 别名]
# 或者: from quopri import decodestring [as 别名]
def test_idempotent_string(self):
for p, e in self.STRINGS:
self.assertEqual(quopri.decodestring(quopri.encodestring(e)), e)