本文整理汇总了Python中email.headerregistry.Address方法的典型用法代码示例。如果您正苦于以下问题:Python headerregistry.Address方法的具体用法?Python headerregistry.Address怎么用?Python headerregistry.Address使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.headerregistry
的用法示例。
在下文中一共展示了headerregistry.Address方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sanitize_address
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def sanitize_address(addr, encoding):
"""
Format a pair of (name, address) or an email address string.
"""
if not isinstance(addr, tuple):
addr = parseaddr(force_text(addr))
nm, addr = addr
localpart, domain = None, None
nm = Header(nm, encoding).encode()
try:
addr.encode('ascii')
except UnicodeEncodeError: # IDN or non-ascii in the local part
localpart, domain = split_addr(addr, encoding)
if six.PY2:
# On Python 2, use the stdlib since `email.headerregistry` doesn't exist.
from email.utils import formataddr
if localpart and domain:
addr = '@'.join([localpart, domain])
return formataddr((nm, addr))
# On Python 3, an `email.headerregistry.Address` object is used since
# email.utils.formataddr() naively encodes the name as ascii (see #25986).
from email.headerregistry import Address
from email.errors import InvalidHeaderDefect, NonASCIILocalPartDefect
if localpart and domain:
address = Address(nm, username=localpart, domain=domain)
return str(address)
try:
address = Address(nm, addr_spec=addr)
except (InvalidHeaderDefect, NonASCIILocalPartDefect):
localpart, domain = split_addr(addr, encoding)
address = Address(nm, username=localpart, domain=domain)
return str(address)
示例2: test_addresses_types
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_addresses_types(self):
source = 'me <[email protected]>'
h = self.make_header('to', source)
self.assertIsInstance(h.addresses, tuple)
self.assertIsInstance(h.addresses[0], Address)
示例3: test_set_from_Address
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_set_from_Address(self):
h = self.make_header('to', Address('me', 'foo', 'example.com'))
self.assertEqual(h, 'me <[email protected]>')
示例4: test_set_from_Address_list
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_set_from_Address_list(self):
h = self.make_header('to', [Address('me', 'foo', 'example.com'),
Address('you', 'bar', 'example.com')])
self.assertEqual(h, 'me <[email protected]>, you <[email protected]>')
示例5: test_set_from_Address_and_Group_list
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_set_from_Address_and_Group_list(self):
h = self.make_header('to', [Address('me', 'foo', 'example.com'),
Group('bing', [Address('fiz', 'z', 'b.com'),
Address('zif', 'f', 'c.com')]),
Address('you', 'bar', 'example.com')])
self.assertEqual(h, 'me <[email protected]>, bing: fiz <[email protected]>, '
'zif <[email protected]>;, you <[email protected]>')
self.assertEqual(h.fold(policy=policy.default.clone(max_line_length=40)),
'to: me <[email protected]>,\n'
' bing: fiz <[email protected]>, zif <[email protected]>;,\n'
' you <[email protected]>\n')
示例6: test_set_from_Group_list
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_set_from_Group_list(self):
h = self.make_header('to', [Group('bing', [Address('fiz', 'z', 'b.com'),
Address('zif', 'f', 'c.com')])])
self.assertEqual(h, 'bing: fiz <[email protected]>, zif <[email protected]>;')
示例7: test_address_username_ro
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_address_username_ro(self):
self._test_attr_ro(Address('foo', 'bar', 'baz'), 'username')
示例8: test_address_domain_ro
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_address_domain_ro(self):
self._test_attr_ro(Address('foo', 'bar', 'baz'), 'domain')
示例9: test_address_from_username_domain
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_address_from_username_domain(self):
a = Address('foo', 'bar', 'baz')
self.assertEqual(a.display_name, 'foo')
self.assertEqual(a.username, 'bar')
self.assertEqual(a.domain, 'baz')
self.assertEqual(a.addr_spec, '[email protected]')
self.assertEqual(str(a), 'foo <[email protected]>')
示例10: test_address_from_addr_spec
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_address_from_addr_spec(self):
a = Address('foo', addr_spec='[email protected]')
self.assertEqual(a.display_name, 'foo')
self.assertEqual(a.username, 'bar')
self.assertEqual(a.domain, 'baz')
self.assertEqual(a.addr_spec, '[email protected]')
self.assertEqual(str(a), 'foo <[email protected]>')
示例11: test_address_with_no_display_name
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_address_with_no_display_name(self):
a = Address(addr_spec='[email protected]')
self.assertEqual(a.display_name, '')
self.assertEqual(a.username, 'bar')
self.assertEqual(a.domain, 'baz')
self.assertEqual(a.addr_spec, '[email protected]')
self.assertEqual(str(a), '[email protected]')
示例12: test_domain_only
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_domain_only(self):
# This isn't really a valid address.
a = Address(domain='buzz')
self.assertEqual(a.display_name, '')
self.assertEqual(a.username, '')
self.assertEqual(a.domain, 'buzz')
self.assertEqual(a.addr_spec, '@buzz')
self.assertEqual(str(a), '@buzz')
示例13: test_username_only
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_username_only(self):
# This isn't really a valid address.
a = Address(username='buzz')
self.assertEqual(a.display_name, '')
self.assertEqual(a.username, 'buzz')
self.assertEqual(a.domain, '')
self.assertEqual(a.addr_spec, 'buzz')
self.assertEqual(str(a), 'buzz')
示例14: test_display_name_only
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_display_name_only(self):
a = Address('buzz')
self.assertEqual(a.display_name, 'buzz')
self.assertEqual(a.username, '')
self.assertEqual(a.domain, '')
self.assertEqual(a.addr_spec, '<>')
self.assertEqual(str(a), 'buzz <>')
示例15: test_quoting
# 需要导入模块: from email import headerregistry [as 别名]
# 或者: from email.headerregistry import Address [as 别名]
def test_quoting(self):
# Ideally we'd check every special individually, but I'm not up for
# writing that many tests.
a = Address('Sara J.', 'bad name', 'example.com')
self.assertEqual(a.display_name, 'Sara J.')
self.assertEqual(a.username, 'bad name')
self.assertEqual(a.domain, 'example.com')
self.assertEqual(a.addr_spec, '"bad name"@example.com')
self.assertEqual(str(a), '"Sara J." <"bad name"@example.com>')