本文整理汇总了Python中email.header.decode_header方法的典型用法代码示例。如果您正苦于以下问题:Python header.decode_header方法的具体用法?Python header.decode_header怎么用?Python header.decode_header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.header
的用法示例。
在下文中一共展示了header.decode_header方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parseaddr_unicode
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def parseaddr_unicode(addr) -> (str, str):
"""Like parseaddr but return name in unicode instead of in RFC 2047 format
'=?UTF-8?B?TmjGoW4gTmd1eeG7hW4=?= <abcd@gmail.com>' -> ('Nhơn Nguyễn', "abcd@gmail.com")
"""
name, email = parseaddr(addr)
email = email.strip().lower()
if name:
name = name.strip()
decoded_string, charset = decode_header(name)[0]
if charset is not None:
try:
name = decoded_string.decode(charset)
except UnicodeDecodeError:
LOG.warning("Cannot decode addr name %s", name)
name = ""
else:
name = decoded_string
return name, email
示例2: decode_rfc2822
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def decode_rfc2822(header_value):
"""Returns the value of the rfc2822 decoded header, or the header_value as-is if it's not encoded."""
result = []
for binary_value, charset in decode_header(header_value):
decoded_value = None
if isinstance(binary_value, str):
result.append(binary_value)
continue
if charset is not None:
try:
decoded_value = binary_value.decode(charset, errors='ignore')
except Exception as e:
logging.warning(f"unable to decode for charset {charset}: {e}")
if decoded_value is None:
try:
decoded_value = binary_value.decode('utf8', errors='ignore')
except Exception as e:
logging.warning(f"unable to decode email header at all (defaulting to hex rep): {e}")
decoded_value = 'HEX({})'.format(binary_value.hex())
result.append(decoded_value)
return ''.join(result)
示例3: test_rfc2047_multiline
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_rfc2047_multiline(self):
eq = self.assertEqual
s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz
foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?="""
dh = decode_header(s)
eq(dh, [
(b'Re: ', None),
(b'r\x8aksm\x9arg\x8cs', 'mac-iceland'),
(b' baz foo bar ', None),
(b'r\x8aksm\x9arg\x8cs', 'mac-iceland')])
header = make_header(dh)
eq(str(header),
'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s')
self.ndiffAssertEqual(header.encode(maxlinelen=76), """\
Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?=
=?mac-iceland?q?=9Arg=8Cs?=""")
示例4: show
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def show(self):
"""
Return mail detailled representation for printing
:return: Representation as :class:`str`
"""
output = "=== Mail %s ===\n" % (self.qid,)
for attr in sorted(dir(self.head)):
if attr.startswith("_"):
continue
value = getattr(self.head, attr)
if not isinstance(value, str):
value = ", ".join(value)
if attr == "Subject":
print(attr, value)
value, enc = header.decode_header(value)[0]
print(enc, attr, value)
if sys.version_info[0] == 2:
value = value.decode(enc) if enc else unicode(value)
output += "%s: %s\n" % (attr, value)
return output
示例5: parse_email_addresses
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def parse_email_addresses(raw_header: str) -> (dict,):
"""
Parse email addresses from header
:param raw_header: example: '=?UTF-8?B?0J7Qu9C1=?= <name@company.ru>,\r\n "\'\\"z, z\\"\'" <imap.tools@ya.ru>'
:return: tuple(dict(name: str, email: str, full: str))
"""
result = []
for raw_name, email in getaddresses([raw_header]):
name = decode_value(*decode_header(raw_name)[0]).strip()
email = email.strip()
if not (name or email):
continue
result.append({
'email': email if '@' in email else '',
'name': name,
'full': '{} <{}>'.format(name, email) if name and email else name or email
})
return tuple(result)
示例6: decode_TEXT
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def decode_TEXT(value):
r"""
Decode :rfc:`2047` TEXT
>>> decode_TEXT("=?utf-8?q?f=C3=BCr?=") == b'f\xfcr'.decode('latin-1')
True
"""
atoms = decode_header(value)
decodedvalue = ''
for atom, charset in atoms:
if charset is not None:
atom = atom.decode(charset)
decodedvalue += atom
return decodedvalue
示例7: test_rfc2047_multiline
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_rfc2047_multiline(self):
eq = self.assertEqual
s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz
foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?="""
dh = decode_header(s)
eq(dh, [
('Re:', None),
('r\x8aksm\x9arg\x8cs', 'mac-iceland'),
('baz foo bar', None),
('r\x8aksm\x9arg\x8cs', 'mac-iceland')])
eq(str(make_header(dh)),
"""Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar
=?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""")
示例8: test_whitespace_eater_unicode
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_whitespace_eater_unicode(self):
eq = self.assertEqual
s = '=?ISO-8859-1?Q?Andr=E9?= Pirard <pirard@dom.ain>'
dh = decode_header(s)
eq(dh, [('Andr\xe9', 'iso-8859-1'), ('Pirard <pirard@dom.ain>', None)])
hu = unicode(make_header(dh)).encode('latin-1')
eq(hu, 'Andr\xe9 Pirard <pirard@dom.ain>')
示例9: test_whitespace_eater_unicode_2
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_whitespace_eater_unicode_2(self):
eq = self.assertEqual
s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?='
dh = decode_header(s)
eq(dh, [('The', None), ('quick brown fox', 'iso-8859-1'),
('jumped over the', None), ('lazy dog', 'iso-8859-1')])
hu = make_header(dh).__unicode__()
eq(hu, u'The quick brown fox jumped over the lazy dog')
示例10: test_rfc2047_missing_whitespace
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_rfc2047_missing_whitespace(self):
s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord'
dh = decode_header(s)
self.assertEqual(dh, [(s, None)])
示例11: test_rfc2047_with_whitespace
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_rfc2047_with_whitespace(self):
s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord'
dh = decode_header(s)
self.assertEqual(dh, [('Sm', None), ('\xf6', 'iso-8859-1'),
('rg', None), ('\xe5', 'iso-8859-1'),
('sbord', None)])
# Test the MIMEMessage class
示例12: test_us_ascii_header
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_us_ascii_header(self):
eq = self.assertEqual
s = 'hello'
x = decode_header(s)
eq(x, [('hello', None)])
h = make_header(x)
eq(s, h.encode())
示例13: test_encoded_adjacent_nonencoded
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_encoded_adjacent_nonencoded(self):
eq = self.assertEqual
h = Header()
h.append('hello', 'iso-8859-1')
h.append('world')
s = h.encode()
eq(s, '=?iso-8859-1?q?hello?= world')
h = make_header(decode_header(s))
eq(h.encode(), s)
示例14: test_whitespace_eater
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_whitespace_eater(self):
eq = self.assertEqual
s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.'
parts = decode_header(s)
eq(parts, [('Subject:', None), ('\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), ('zz.', None)])
hdr = make_header(parts)
eq(hdr.encode(),
'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.')
示例15: test_broken_base64_header
# 需要导入模块: from email import header [as 别名]
# 或者: from email.header import decode_header [as 别名]
def test_broken_base64_header(self):
raises = self.assertRaises
s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3I ?='
raises(errors.HeaderParseError, decode_header, s)
# Test RFC 2231 header parameters (en/de)coding