免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1711 | 回复: 0
打印 上一主题 下一主题

Python网络编程基础笔记-信息头编码与解码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-14 20:48 |只看该作者 |倒序浏览
1.对信息头进行编码和解码
               
               
                # -*- coding:cp936 -*-
#!/usr/bin/env python
"""
对信息头进行编码和解码操作
"""
import sys,email,codecs
from email import Header
msg = email.message_from_file(file("JCParseMIME.email"))
for header,value in msg.items():
    headerparts = Header.decode_header(value)
    headval = []
    for part in headerparts:
        data,charset = part
    if charset is None:
        charset = "ascii"
    dec = codecs.getdecoder(charset)
    enc = codecs.getencoder("iso-8859-1")
    data = enc(dec(data)[0])[0]
    headval.append(data)
    print("%s:%s"%(header,"".join(headval)))
2.email内容
Content-Type: multipart/mixed; boundary="===============0874728357=="
MIME-Version: 1.0
--===============0874728357==
Content-Type: multipart/alternative; boundary="===============1169888608=="
MIME-Version: 1.0
--===============1169888608==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Hello,This is nested message test.
---jcodeer
--===============1169888608==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Hell,
This is bold test messsage test.
---jcodeer
--===============1169888608==--
--===============0874728357==
Content-Type: text/x-python; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attach; filename="JCNestedMIME.py"
# -*- coding:cp936 -*-
#!/usr/bin/env python
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Utils,Encoders
import mimetypes,sys
def genpart(data,contenttype):
    maintype,subtype = contenttype.split("/")
    if maintype == "text":
        retval = MIMEText(data,_subtype = subtype)
    else:
        retval = MIMEBase(maintype,subtype)
        retval.set_payload(data)
        Encoders.encode_base64(retval)
    return retval
def attachment(filename):
    fd = file(filename,"rb")
    mimetype,mimeencoding = mimetypes.guess_type(filename)
    if mimeencoding or (mimetype is None):
        mimetype = "application/octet-stream"
    retval = genpart(fd.read(),mimetype)
    retval.add_header("Content-Disposition","attach",filename = filename)
    fd.close()
    return retval
messagetext = """
Hello,This is nested message test.
---jcodeer
"""
messagehtml = """
Hell,
This is bold test messsage test.
---jcodeer
"""
msg = MIMEMultipart()
body = MIMEMultipart("alternative")
body.attach(genpart(messagetext,"text/plain"))
body.attach(genpart(messagehtml,"text/html"))
msg.attach(body)
msg.attach(attachment("JCNestedMIME.py"))
print msg.as_string()
--===============0874728357==--


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19742/showart_422400.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP