smtpd
— SMTP (简单邮件传输协议) 服务器
¶
源代码: Lib/smtpd.py
此模块提供实现 SMTP (电子邮件) 服务器的几个类。
存在几种服务器实现;一个是可以覆盖且什么都不做的一般实现,而其它 2 个提供特定邮件发送战略。
此外,可以扩展 SMTPChannel 以实现非常具体的交互行为采用 SMTP (简单邮件传输协议) 客户端。
代码支持 RFC 5321 ,加 RFC 1870 SIZE 和 RFC 6531 SMTPUTF8 扩展。
smtpd.
SMTPServer
(
localaddr
,
remoteaddr
,
data_size_limit=33554432
,
map=None
,
enable_SMTPUTF8=False
,
decode_data=False
)
¶
创建新的
SMTPServer
对象,绑定到本地地址
localaddr
。它将视
remoteaddr
为上游 SMTP (简单邮件传输协议) 中继器。
localaddr
and
remoteaddr
应该为
(host, port)
元组。对象继承自
asyncore.dispatcher
,且因此将自身插入
asyncore
的实例化事件循环。
data_size_limit
指定将接受的最大字节数在
DATA
命令。值为
None
or
0
意味着没有限制。
map
是用于连接的套接字映射 (最初空字典是合适值)。若未指定
asyncore
全局套接字映射被使用。
enable_SMTPUTF8
确定是否
SMTPUTF8
扩展 (作为定义在
RFC 6531
) 应该启用。默认为
False
。当
True
,
SMTPUTF8
被接受作为参数到
MAIL
命令和当呈现被传递给
process_message()
在
kwargs['mail_options']
列表。
decode_data
and
enable_SMTPUTF8
不可以设为
True
在同一时间。
decode_data
specifies whether the data portion of the SMTP transaction should be decoded using UTF-8. When
decode_data
is
False
(默认),服务器广告
8BITMIME
扩展 (
RFC 6152
),接受
BODY=8BITMIME
参数到
MAIL
command, and when present passes it to
process_message()
在
kwargs['mail_options']
列表。
decode_data
and
enable_SMTPUTF8
不可以设为
True
在同一时间。
process_message
(
peer
,
mailfrom
,
rcpttos
,
data
,
**kwargs
)
¶
引发
NotImplementedError
exception. Override this in subclasses to do something useful with this message. Whatever was passed in the constructor as
remoteaddr
will be available as the
_remoteaddr
属性。
peer
是远程主机的地址,
mailfrom
是信封发起人,
rcpttos
are the envelope recipients and
data
is a string containing the contents of the e-mail (which should be in
RFC 5321
格式)。
若
decode_data
构造函数关键词被设为
True
,
data
自变量将是 Unicode 字符串。若它被设为
False
,它将是字节对象。
kwargs
是包含额外信息的字典。它为空若
decode_data=True
给定作为初始自变量,否则它包含下列键:
- mail_options :
接收的所有参数列表到
['BODY=8BITMIME', 'SMTPUTF8']).- rcpt_options :
如同 mail_options 但对于
RCPTcommand. Currently noRCPT TOoptions are supported, so for now this will always be an empty list.
实现的
process_message
应该使用
**kwargs
signature to accept arbitrary keyword arguments, since future feature enhancements may add keys to the kwargs dictionary.
返回
None
以请求正常
250 Ok
响应;否则返回期望响应字符串按
RFC 5321
格式。
channel_class
¶
在子类中覆盖此以使用自定义
SMTPChannel
为管理 SMTP (简单邮件传输协议) 客户端。
3.4 版新增: map 构造函数自变量。
3.5 版改变: localaddr and remoteaddr 现在可以包含 IPv6 地址。
3.5 版新增:
decode_data
and
enable_SMTPUTF8
构造函数参数,和
kwargs
参数到
process_message()
when
decode_data
is
False
.
3.6 版改变:
decode_data
现为
False
在默认情况下。
smtpd.
DebuggingServer
(
localaddr
,
remoteaddr
)
¶
Create a new debugging server. Arguments are as per
SMTPServer
. Messages will be discarded, and printed on stdout.
smtpd.
PureProxy
(
localaddr
,
remoteaddr
)
¶
Create a new pure proxy server. Arguments are as per
SMTPServer
. Everything will be relayed to
remoteaddr
. Note that running this has a good chance to make you into an open relay, so please be careful.
smtpd.
MailmanProxy
(
localaddr
,
remoteaddr
)
¶
从 3.9 版起弃用,将在 3.11 版中移除:
MailmanProxy
is deprecated, it depends on a
Mailman
module which no longer exists and therefore is already broken.
Create a new pure proxy server. Arguments are as per
SMTPServer
. Everything will be relayed to
remoteaddr
, unless local mailman configurations knows about an address, in which case it will be handled via mailman. Note that running this has a good chance to make you into an open relay, so please be careful.
smtpd.
SMTPChannel
(
server
,
conn
,
addr
,
data_size_limit=33554432
,
map=None
,
enable_SMTPUTF8=False
,
decode_data=False
)
¶
创建新的
SMTPChannel
object which manages the communication between the server and a single SMTP client.
conn and addr are as per the instance variables described below.
data_size_limit
指定将接受的最大字节数在
DATA
命令。值为
None
or
0
意味着没有限制。
enable_SMTPUTF8
确定是否
SMTPUTF8
扩展 (作为定义在
RFC 6531
) 应该启用。默认为
False
.
decode_data
and
enable_SMTPUTF8
不可以设为
True
在同一时间。
A dictionary can be specified in map to avoid using a global socket map.
decode_data
specifies whether the data portion of the SMTP transaction should be decoded using UTF-8. The default is
False
.
decode_data
and
enable_SMTPUTF8
不可以设为
True
在同一时间。
To use a custom SMTPChannel implementation you need to override the
SMTPServer.channel_class
of your
SMTPServer
.
3.5 版改变: decode_data and enable_SMTPUTF8 参数被添加。
3.6 版改变:
decode_data
现为
False
在默认情况下。
SMTPChannel
拥有下列实例变量:
smtp_server
¶
保持
SMTPServer
卵生此通道。
conn
¶
保存连接到客户端的套接字对象。
addr
¶
Holds the address of the client, the second value returned by
socket.accept
received_lines
¶
Holds a list of the line strings (decoded using UTF-8) received from the client. The lines have their
"\r\n"
line ending translated to
"\n"
.
smtp_state
¶
Holds the current state of the channel. This will be either
COMMAND
initially and then
DATA
after the client sends a “DATA” line.
seen_greeting
¶
Holds a string containing the greeting sent by the client in its “HELO”.
mailfrom
¶
Holds a string containing the address identified in the “MAIL FROM:” line from the client.
rcpttos
¶
Holds a list of strings containing the addresses identified in the “RCPT TO:” lines from the client.
received_data
¶
Holds a string containing all of the data sent by the client during the DATA state, up to but not including the terminating
"\r\n.\r\n"
.
fqdn
¶
Holds the fully-qualified domain name of the server as returned by
socket.getfqdn()
.
SMTPChannel
operates by invoking methods named
smtp_<command>
upon reception of a command line from the client. Built into the base
SMTPChannel
class are methods for handling the following commands (and responding to them appropriately):
|
命令 |
Action taken |
|---|---|
|
HELO |
Accepts the greeting from the client and stores it in
|
|
EHLO |
Accepts the greeting from the client and stores it in
|
|
NOOP |
Takes no action. |
|
QUIT |
干净地关闭连接。 |
|
|
Accepts the “MAIL FROM:” syntax and stores the supplied address as
|
|
RCPT |
Accepts the “RCPT TO:” syntax and stores the supplied addresses in the
|
|
RSET |
重置
|
|
DATA |
Sets the internal state to
|
|
HELP |
Returns minimal information on command syntax |
|
VRFY |
Returns code 252 (the server doesn’t know if the address is valid) |
|
EXPN |
Reports that the command is not implemented. |