Python网络编程基础笔记-在头信息中使用非ASCII字符

1.使用ISO-8859-1编码显示非ASCII字符
               
               
                # -*- coding:cp936 -*-
#!/usr/bin/env python
"""
在头信息中使用非ASCII码
1.先生成一个Header对象
2.再将header与内容关联上
3.将Header赋值给相应的属性
"""
from email.MIMEText import MIMEText
from email.Header import Header
from email import Utils
message = """
Hello,
    This is Non-English header test.
---jcodeer
"""
msg = MIMEText(message)
msg["To"] = "jcodeer@126.com"
# 创建Header对象(1)
header = Header("Michael M\xfccller","iso-8859-1")
# 使用append将名称与地址关联(2)
header.append("jcodeer@hotmail","ascii")
# 将From属性设置为header
msg["From"] = header
print msg.as_string()
2.输出结果
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
To: jcodeer@126.com
From: =?iso-8859-1?q?Michael_M=FCcller?= jcodeer@hotmail
Hello,
    This is Non-English header test.
---jcodeer