Skip to Content
《Python 概论》,第 4 版
book

《Python 概论》,第 4 版

by Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire
May 2025
Intermediate to advanced
738 pages
9h 28m
Chinese
O'Reilly Media, Inc.
Content preview from 《Python 概论》,第 4 版

第 21 章 电子邮件、MIME 和其他网络编码 电子邮件、MIME 和其他网络编码

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在网络上传输的是字节流,也就是网络术语中的八进制。当然,字节可以通过几种可能的编码方式中的任何一种来表示文本。然而,你想通过网络发送的内容往往不仅仅是文本或字节流,还有更多的结构。 多用途互联网邮件扩展(MIME)和其他编码标准规定了如何将结构化数据表示为字节或文本,从而弥补了这一差距。虽然这些编码最初是为电子邮件设计的,但也用于 Network+ 和许多其他网络系统。Python 通过各种库模块支持此类编码,例如base64quopriuu(在"将二进制数据编码为 ASCII 文本 "中介绍,以及电子邮件包的模块(在下一节中介绍)。例如,这些编码允许我们以一种编码无缝创建包含另一种编码附件的邮件,从而避免了许多繁琐的工作。

MIME 和电子邮件格式处理

电子邮件包处理 MIME 文件的解析、生成和操作,如电子邮件消息、Network News Transfer Protocol (NNTP) 帖子、HTTP 交互等。Python 标准库还包含处理这些工作中某些部分的其他模块。然而,email包为这些重要任务提供了一个完整而系统的方法。我们建议您使用电子邮件,而不是那些与电子邮件部分功能重叠的旧模块。电子邮件,尽管它的名字叫电子邮件,但它与接收或发送电子邮件毫无关系;关于这类任务,请参阅模块imaplibpoplibsmtplib,它们将在"电子邮件协议" 中介绍。相反,email处理的是接收 MIME 消息(可能是邮件,也可能不是)后的处理工作,或发送前的正确构造工作。

电子邮件软件包中的功能

电子邮件包提供了四个工厂函数,可从字符串或文件返回类email.message.Message的实例m(见表 21-1)。这些函数依赖于类email.parser.Parser,但工厂函数更方便、更简单。因此,我们在本书中不再进一步介绍email.parser模块。

表 21-1. 从字符串或文件构建邮件对象的邮件工厂函数
来自二进制文件的消息 message_from_binary_file(f) 通过解析二进制文件类对象f 的内容生成m,该对象必须是开放的,以便读取
消息来自字节 message_from_bytes(s) 通过解析字节字符串s生成m
来自文件的消息 message_from_file(f) 通过解析文本文件类对象f 的内容生成m,该对象必须已打开供读取
消息来自字符串 message_from_string(s) 通过解析字符串s生成m

电子邮件信息模块

email.message模块提供Message 类。电子邮件软件包的所有部分都制作、修改或使用Message 实例。Message的实例m是 MIME 消息的模型,包括 标头有效载荷(数据内容)。m是一个映射,标头名称是键,标头值字符串是值。

要创建一个初始为空的m,可以调用不带参数的Messagem的有效载荷可以是字符串、Message的其他单个实例或 多部分消息(其他Message实例的递归嵌套列表

您可以在创建的电子邮件中任意设置标题。一些互联网 RFC 规定了各种用途的标题。主要适用的 RFC 是RFC 2822;你可以在非规范性的 RFC2076 中找到许多其他 RFC 的摘要。

为了使m更方便,它作为映射的语义与 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
ppk on JavaScript

ppk on JavaScript

Peter-Paul Koch

Publisher Resources

ISBN: 9798341656840