poplib — POP3 协议客户端

源代码: Lib/poplib.py


此模块定义类, POP3 ,封装到 POP3 服务器的连接并实现协议作为定义在 RFC 1939 POP3 类支持的最小和可选命令集两者来自 RFC 1939 POP3 类还支持 STLS 命令引入在 RFC 2595 在已建立的连接中启用加密通信。

另外,此模块还提供类 POP3_SSL ,提供连接到使用 SSL (安全套接字层) 作为底层协议层的 POP3 服务器的支持。

注意,虽然 POP3 支持广泛,但已过时。POP3 服务器的实现质量差别很大,且很多很差。若邮件服务器支持 IMAP (Internet 消息访问协议),最好使用 imaplib.IMAP4 类,因为 IMAP 服务器趋向更好实现。

poplib 模块提供 2 个类:

class poplib. POP3 ( host , port=POP3_PORT [ , timeout ] )

此类实现实际 POP3 协议。 连接的创建在初始化实例时。若 port 被省略,使用标准 POP3 端口 110。可选 timeout 参数指定试图连接的超时 (以秒为单位),若未指定,将使用全局默认超时设置。

引发 审计事件 poplib.connect 采用自变量 self , host , port .

所有命令将引发 审计事件 poplib.putline 采用自变量 self and line ,其中 line 是即将发送给远程主机的字节。

3.9 版改变: timeout 参数被设为 0,它将引发 ValueError 以阻止非阻塞套接字的创建。

class poplib. POP3_SSL ( host , port=POP3_SSL_PORT , keyfile=None , certfile=None , timeout=None , context=None )

这是子类化的 POP3 通过 SSL (安全套接字层) 加密套接字连接到服务器。若 port 未指定,使用标准 POP3-over-SSL 端口 995。 timeout 工作如在 POP3 构造函数。 context 是可选 ssl.SSLContext 对象允许捆绑 SSL 配置选项、证书及私钥成单一 (潜在长期存活) 结构。请阅读 安全注意事项 了解最佳实践。

keyfile and certfile 是传统替代对于 context - they can point to PEM-formatted private key and certificate chain files, respectively, for the SSL connection.

引发 审计事件 poplib.connect 采用自变量 self , host , port .

所有命令将引发 审计事件 poplib.putline 采用自变量 self and line ,其中 line 是即将发送给远程主机的字节。

3.2 版改变: context 参数被添加。

3.4 版改变: 类现在支持主机名校验采用 ssl.SSLContext.check_hostname and SNI (服务器名称指示) (见 ssl.HAS_SNI ).

从 3.6 版起弃用: keyfile and certfile 弃用代之 context 。请使用 ssl.SSLContext.load_cert_chain() 代替,或让 ssl.create_default_context() 为您选择系统的受信任 CA 证书。

3.9 版改变: timeout 参数被设为 0,它将引发 ValueError 以阻止非阻塞套接字的创建。

定义一异常作为属性对于 poplib 模块:

exception poplib. error_proto

来自此模块的任何错误引发的异常 (错误来自 socket 模块未捕获的)。异常原因以字符串形式被传递给构造函数。

另请参阅

模块 imaplib

标准 Python IMAP 模块。

关于抓取邮件的常见问题

The FAQ for the fetchmail POP/IMAP client collects information on POP3 server variations and RFC noncompliance that may be useful if you need to write an application based on the POP protocol.

POP3 对象

所有 POP3 命令都按小写由同名方法表示;大多数返回由服务器发送的响应文本。

POP3 实例具有下列方法:

POP3. set_debuglevel ( level )

Set the instance’s debugging level. This controls the amount of debugging output printed. The default, 0 , produces no debugging output. A value of 1 produces a moderate amount of debugging output, generally a single line per request. A value of 2 or higher produces the maximum amount of debugging output, logging each line sent and received on the control connection.

POP3. getwelcome ( )

返回由 POP3 服务器发送的问候字符串。

POP3. capa ( )

查询服务器的能力作为指定在 RFC 2449 . Returns a dictionary in the form {'name': ['param'...]} .

3.4 版新增。

POP3. user ( username )

Send user command, response should indicate that a password is required.

POP3. pass_ ( password )

Send password, response includes message count and mailbox size. Note: the mailbox on the server is locked until quit() 被调用。

POP3. apop ( user , secret )

Use the more secure APOP authentication to log into the POP3 server.

POP3. rpop ( user )

Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.

POP3. stat ( )

Get mailbox status. The result is a tuple of 2 integers: (message count, mailbox size) .

POP3. list ( [ which ] )

请求消息列表,结果按形式 (response, ['mesg_num octets', ...], octets) 。若 which 有设置,它是要列表的消息。

POP3. retr ( which )

Retrieve whole message number which , and set its seen flag. Result is in form (response, ['line', ...], octets) .

POP3. dele ( which )

Flag message number which for deletion. On most servers deletions are not actually performed until QUIT (the major exception is Eudora QPOP, which deliberately violates the RFCs by doing pending deletes on any disconnect).

POP3. rset ( )

移除邮箱的任何删除标记。

POP3. noop ( )

什么都不做。可能用作保持存活。

POP3. quit ( )

Signoff: commit changes, unlock mailbox, drop connection.

POP3. top ( which , howmuch )

Retrieves the message header plus howmuch lines of the message after the header of message number which . Result is in form (response, ['line', ...], octets) .

The POP3 TOP command this method uses, unlike the RETR command, doesn’t set the message’s seen flag; unfortunately, TOP is poorly specified in the RFCs and is frequently broken in off-brand servers. Test this method by hand against the POP3 servers you will use before trusting it.

POP3. uidl ( which=None )

Return message digest (unique id) list. If which is specified, result contains the unique id for that message in the form 'response mesgnum uid ,否则结果是列表 (response, ['mesgnum uid', ...], octets) .

POP3. utf8 ( )

Try to switch to UTF-8 mode. Returns the server response if successful, raises error_proto if not. Specified in RFC 6856 .

3.5 版新增。

POP3. stls ( context=None )

Start a TLS session on the active connection as specified in RFC 2595 . This is only allowed before user authentication

context parameter is a ssl.SSLContext 对象允许捆绑 SSL 配置选项、证书及私钥成单一 (潜在长期存活) 结构。请阅读 安全注意事项 了解最佳实践。

此方法支持主机名校验凭借 ssl.SSLContext.check_hostname and SNI (服务器名称指示) (见 ssl.HAS_SNI ).

3.4 版新增。

实例化的 POP3_SSL 不拥有额外方法。此子类的接口等同于其父级。

POP3 范例

这里是 (不带错误校验) 打开邮箱、检索和打印所有消息的最小范例:

import getpass, poplib
M = poplib.POP3('localhost')
M.user(getpass.getuser())
M.pass_(getpass.getpass())
numMessages = len(M.list()[1])
for i in range(numMessages):
    for j in M.retr(i+1)[1]:
        print(j)
								

At the end of the module, there is a test section that contains a more extensive example of usage.

内容表

上一话题

ftplib — FTP (文件传输协议) 客户端

下一话题

imaplib — IMAP4 协议客户端

本页