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 版

第 19 章 客户端网络协议模块 客户端网络协议模块

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

Python 的标准库 提供了多个模块,用于简化客户端和服务器端互联网协议的使用。如今, Python Package Index(最著名的PyPI)提供了更多这样的软件包。由于许多标准库模块可以追溯到上个世纪,您会发现现在的第三方软件包支持更广泛的协议,其中一些提供了比标准库更好的 API。当你需要使用标准库中缺少的网络协议,或者标准库中涵盖的协议你认为不尽人意时,一定要搜索 PyPI--你很可能在那里找到更好的解决方案。

在本章中,我们将介绍一些允许相对简单地使用 Network+ 协议的标准库包:这些库包可以让你在不需要第三方包的情况下编写代码,使你的应用程序或库更容易安装到其他机器上。因此,您在处理遗留代码时可能会遇到它们,它们的简单性也使 Python 学生对它们产生了阅读兴趣。我们还提到了一些第三方软件包,它们涵盖了标准库中未包含的重要网络协议,但我们不涉及使用异步编程的第三方软件包。

对于 HTTP 客户端和其他最好通过 URL 访问的网络资源(如匿名 FTP 网站)这种非常频繁的使用情况,Python 文档中甚至推荐使用第三方请求,因此我们涵盖了这一点,并推荐使用它来代替标准库模块。

电子邮件协议

目前,大多数 电子邮件都是通过执行 简单邮件传输协议 (SMTP) 的服务器发送的,通过使用 邮局协议版本 3 (POP3) 和/或 互联网消息访问协议版本 4 (IMAP4) 的服务器和客户端接收的。1这些协议的客户端分别由 Python 标准库模块smtplibpoplibimaplib 支持,我们将在本书中介绍前两个模块。当您需要处理解析生成电子邮件时,请使用第 21 章中介绍的电子邮件包。

如果您需要编写一个可以通过 POP3 或 IMAP4 连接的客户端,标准的建议是选择 IMAP4,因为它功能更强大,而且根据 Python 自己的在线文档,它在服务器端的实现往往更准确。不幸的是,imaplib非常复杂,本书无法涵盖。如果你选择走这条路,请使用在线文档,同时不可避免地辅以 IMAP RFC,可能的话也可以使用其他相关的 RFC,如关于功能的 5161 和 6855 以及关于命名空间的 2342。除了标准库模块的在线文档外,还不能避免使用 RFC:传递给imaplib函数和方法的许多参数以及调用它们的结果都是字符串,其格式只在 RFC 中有记录,而不是在 Python 自己的文档中。强烈建议使用更简单、抽象级别更高的第三方软件包IMAPClient,只需通过pip 安装即可使用,网上也有详细的文档说明。

poplib 模块

poplib模块提供了一个用于访问 POP 邮箱的类POP32构造函数的签名如下

POP3 POP3(主机,端口=110) 返回连接到指定主机端口的 POP3类实例p。类POP3_SSL的行为与此相同,但它通过安全的 TLS 通道连接主机(默认端口为 995);连接到要求最低安全性的电子邮件服务器(如pop.gmail.com)时需要使用它。a

a特别是要连接到 Gmail 账户,你需要对该账户进行配置,启用 POP、"允许安全性较低的应用程序 "并避免两步验证--我们一般不建议这样做,因为它们会削弱电子邮件的安全性。

POP3类的实例p提供了许多方法;表 19-1 列出了最常用的方法。在每种情况下,msgnum ...

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