本文整理汇总了Python中email.Charset.Charset方法的典型用法代码示例。如果您正苦于以下问题:Python Charset.Charset方法的具体用法?Python Charset.Charset怎么用?Python Charset.Charset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.Charset
的用法示例。
在下文中一共展示了Charset.Charset方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addr_header_encode
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def addr_header_encode(text, header_name=None):
"""Encode and line-wrap the value of an email header field containing
email addresses."""
# Convert to unicode, if required.
if not isinstance(text, unicode):
text = unicode(text, "utf-8")
text = ", ".join(
formataddr((header_encode(name), emailaddr))
for name, emailaddr in getaddresses([text])
)
if is_ascii(text):
charset = "ascii"
else:
charset = "utf-8"
return Header(
text, header_name=header_name, charset=Charset(charset)
).encode()
示例2: test_getset_charset
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_getset_charset(self):
eq = self.assertEqual
msg = Message()
eq(msg.get_charset(), None)
charset = Charset('iso-8859-1')
msg.set_charset(charset)
eq(msg['mime-version'], '1.0')
eq(msg.get_content_type(), 'text/plain')
eq(msg['content-type'], 'text/plain; charset="iso-8859-1"')
eq(msg.get_param('charset'), 'iso-8859-1')
eq(msg['content-transfer-encoding'], 'quoted-printable')
eq(msg.get_charset().input_charset, 'iso-8859-1')
# Remove the charset
msg.set_charset(None)
eq(msg.get_charset(), None)
eq(msg['content-type'], 'text/plain')
# Try adding a charset when there's already MIME headers present
msg = Message()
msg['MIME-Version'] = '2.0'
msg['Content-Type'] = 'text/x-weird'
msg['Content-Transfer-Encoding'] = 'quinted-puntable'
msg.set_charset(charset)
eq(msg['mime-version'], '2.0')
eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"')
eq(msg['content-transfer-encoding'], 'quinted-puntable')
示例3: test__all__
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test__all__(self):
module = __import__('email')
all = module.__all__
all.sort()
self.assertEqual(all, [
# Old names
'Charset', 'Encoders', 'Errors', 'Generator',
'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
'MIMENonMultipart', 'MIMEText', 'Message',
'Parser', 'Utils', 'base64MIME',
# new names
'base64mime', 'charset', 'encoders', 'errors', 'generator',
'header', 'iterators', 'message', 'message_from_file',
'message_from_string', 'mime', 'parser',
'quopriMIME', 'quoprimime', 'utils',
])
示例4: test_charset_richcomparisons
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_charset_richcomparisons(self):
eq = self.assertEqual
ne = self.assertNotEqual
cset1 = Charset()
cset2 = Charset()
eq(cset1, 'us-ascii')
eq(cset1, 'US-ASCII')
eq(cset1, 'Us-AsCiI')
eq('us-ascii', cset1)
eq('US-ASCII', cset1)
eq('Us-AsCiI', cset1)
ne(cset1, 'usascii')
ne(cset1, 'USASCII')
ne(cset1, 'UsAsCiI')
ne('usascii', cset1)
ne('USASCII', cset1)
ne('UsAsCiI', cset1)
eq(cset1, cset2)
eq(cset2, cset1)
示例5: test_get_body_encoding_with_uppercase_charset
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_get_body_encoding_with_uppercase_charset(self):
eq = self.assertEqual
msg = Message()
msg['Content-Type'] = 'text/plain; charset=UTF-8'
eq(msg['content-type'], 'text/plain; charset=UTF-8')
charsets = msg.get_charsets()
eq(len(charsets), 1)
eq(charsets[0], 'utf-8')
charset = Charset(charsets[0])
eq(charset.get_body_encoding(), 'base64')
msg.set_payload('hello world', charset=charset)
eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n')
eq(msg.get_payload(decode=True), 'hello world')
eq(msg['content-transfer-encoding'], 'base64')
# Try another one
msg = Message()
msg['Content-Type'] = 'text/plain; charset="US-ASCII"'
charsets = msg.get_charsets()
eq(len(charsets), 1)
eq(charsets[0], 'us-ascii')
charset = Charset(charsets[0])
eq(charset.get_body_encoding(), Encoders.encode_7or8bit)
msg.set_payload('hello world', charset=charset)
eq(msg.get_payload(), 'hello world')
eq(msg['content-transfer-encoding'], '7bit')
示例6: test_charset_richcomparisons
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_charset_richcomparisons(self):
eq = self.assertEqual
ne = self.failIfEqual
cset1 = Charset()
cset2 = Charset()
eq(cset1, 'us-ascii')
eq(cset1, 'US-ASCII')
eq(cset1, 'Us-AsCiI')
eq('us-ascii', cset1)
eq('US-ASCII', cset1)
eq('Us-AsCiI', cset1)
ne(cset1, 'usascii')
ne(cset1, 'USASCII')
ne(cset1, 'UsAsCiI')
ne('usascii', cset1)
ne('USASCII', cset1)
ne('UsAsCiI', cset1)
eq(cset1, cset2)
eq(cset2, cset1)
示例7: header_encode
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def header_encode(text, header_name=None):
"""Encode and line-wrap the value of an email header field."""
# Convert to unicode, if required.
if not isinstance(text, unicode):
text = unicode(text, "utf-8")
if is_ascii(text):
charset = "ascii"
else:
charset = "utf-8"
return Header(
text, header_name=header_name, charset=Charset(charset)
).encode()
示例8: test_set_payload_with_charset
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_set_payload_with_charset(self):
msg = Message()
charset = Charset('iso-8859-1')
msg.set_payload('This is a string payload', charset)
self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1')
示例9: test_charsets_case_insensitive
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_charsets_case_insensitive(self):
lc = Charset('us-ascii')
uc = Charset('US-ASCII')
self.assertEqual(lc.get_body_encoding(), uc.get_body_encoding())
示例10: test_encode
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def test_encode(self):
eq = self.assertEqual
eq(quopriMIME.encode(''), '')
eq(quopriMIME.encode('hello'), 'hello')
# Test the binary flag
eq(quopriMIME.encode('hello\r\nworld'), 'hello\nworld')
eq(quopriMIME.encode('hello\r\nworld', 0), 'hello\nworld')
# Test the maxlinelen arg
eq(quopriMIME.encode('xxxx ' * 20, maxlinelen=40), """\
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=
x xxxx xxxx xxxx xxxx=20""")
# Test the eol argument
eq(quopriMIME.encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=\r
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=\r
x xxxx xxxx xxxx xxxx=20""")
eq(quopriMIME.encode("""\
one line
two line"""), """\
one line
two line""")
# Test the Charset class
示例11: tearDown
# 需要导入模块: from email import Charset [as 别名]
# 或者: from email.Charset import Charset [as 别名]
def tearDown(self):
from email import Charset as CharsetModule
try:
del CharsetModule.CHARSETS['fake']
except KeyError:
pass