aiosmtpd - 基于 asyncio 的 SMTP 服务器

Python 标准库包括基本 SMTP 服务器在 smtpd 模块,基于旧的异步库 asyncore and asynchat 。这些模块非常旧且它们的年龄肯定很明显; asyncore and asynchat 是难以使用、理解、扩展及修复的 API。

采用引入的 asyncio 模块在 Python 3.4,更好的异步 I/O 方式现在可用。显然,Python 3 需要基于 asyncio 的 SMTP 版本和相关协议。此工程把几个经验丰富的 Python 开发者聚到一起合作重实现这。

此包提供 SMTP 和 LMTP 协议的这种实现。

完整文档获取自 aiosmtpd.readthedocs.io

要求

需要 至少 Python 3.6 以使用此库。

支持平台

aiosmtpd 有测试在 CPython and PyPy3.7 对于下列平台 (按字母顺序排序):

  • Cygwin (在 Windows 10) [1]

  • FreeBSD 12 [2]

  • OpenSUSE 飞跃 15 [2]

  • Ubuntu 18.04

  • Ubuntu 20.04

  • Windows 10

    [1] 仅支持由 Cygwin 提供的 CPython 版本 [2] 仅支持最新次要发行

aiosmtpd probably 可以运行在上文未列出的平台中,但我们无法为未列出的平台提供支持。

安装

安装通常采用 pip :

pip install aiosmtpd
					

若收到错误消息 ModuleNotFoundError: No module named 'public' ,可能意味着您的 setuptools 太旧;试着升级 setuptools 到至少版本 46.4.0 其有 实现修复对于此问题 .

工程细节

As of 2016-07-14, aiosmtpd has been put under the aio-libs umbrella project and moved to GitHub.

The best way to contact the developers is through the GitHub links above. You can also request help by submitting a question on StackOverflow.

构建

You can install this package in a virtual environment like so:

$ python3 -m venv /path/to/venv
$ source /path/to/venv/bin/activate
$ python setup.py install
					

This will give you a command line script called aiosmtpd which implements the SMTP server. Use aiosmtpd --help for a quick reference.

You will also have access to the aiosmtpd library, which you can use as a testing environment for your SMTP clients. See the documentation links above for details.

开发

You’ll need the tox tool to run the test suite for Python 3. Once you’ve got that, run:

$ tox
					

Individual tests can be run like this:

$ tox -- <testname>
					

where <testname> is the “node id” of the test case to run, as explained in the pytest documentation . The command above will run that one test case against all testenvs defined in tox.ini (see below).

If you want test to stop as soon as it hit a failure, use the -x / --exitfirst 选项:

$ tox -- -x
					

还可以添加 -s / --capture=no 选项以展示输出,如:

$ tox -e py36-nocov -- -s
					

且这些选项可以组合:

$ tox -e py36-nocov -- -x -s <testname>
					

( -e parameter is explained in the next section about ‘testenvs’. In general, you’ll want to choose the nocov testenvs if you want to show output, so you can see which test is generating which output.)

-x and -s 选项可以组合:

$ tox -e py36-nocov -- -x -s <testname>
					

Supported ‘testenvs’

一般而言, -e parameter to tox specifies one (or more) testenv to run (separate using comma if more than one testenv). The following testenvs have been configured and tested:

  • {py36,py37,py38,py39,pypy3}-{nocov,cov,diffcov,profile}

    Specifies the interpreter to run and the kind of testing to perform.

    • nocov = no coverage testing. Tests will run verbosely.

    • cov = with coverage testing. Tests will run in brief mode (showing a single character per test run)

    • diffcov = with diff-coverage report (showing difference in coverage compared to previous commit). Tests will run in brief mode

    • profile = no coverage testing, but code profiling instead. This must be invoked manually 使用 -e 参数

    Note 1: As of 2021-02-23, only the {py36,py38}-{nocov,cov} combinations work on Cygwin .

    Note 2: It is also possible to use whatever Python version is used when invoking tox by using the py target, but you must explicitly include the type of testing you want. For example:

    $ tox -e "py-{nocov,cov,diffcov}"
    							

    (Don’t forget the quotes if you want to use braces!)

    You might want to do this for CI platforms where the exact Python version is pre-prepared, such as Travis CI or GitHub Actions ; this will definitely save some time during tox’s testenv prepping.

    For all testenv combinations except diffcov, bandit security check will also be run prior to running pytest.

  • qa

    履行 flake8 代码风格校验,和 flake8-bugbear 设计校验。

    In addition, some tests to help ensure that aiosmtpd is releasable to PyPI are also run.

  • docs

    Builds HTML documentation and manpage using Sphinx. A pytest doctest will run prior to actual building of the documentation.

  • static

    Performs a static type checking 使用 pytype .

    Note 1: Please ensure that all pytype dependencies have been installed before executing this testenv.

    Note 2: This testenv will be _SKIPPED_ on Windows, because pytype currently cannot run on Windows.

    Note 3: This testenv does NOT work on Cygwin .

环境变量

ASYNCIO_CATCHUP_DELAY

Due to how asyncio event loop works, some actions do not instantly get responded to. This is especially so on slower / overworked systems. In consideration of such situations, some test cases invoke a slight delay to let the event loop catch up.

默认为 0.1 and can be set to any float value you want.

不同 Python 版本

The tox configuration files have been created to cater for more than one Python versions safely : If an interpreter is not found for a certain Python version, tox will skip that whole testenv.

However, with a little bit of effort, you can have multiple Python interpreter versions on your system by using pyenv . General steps:

  1. 安装 pyenv from https://github.com/pyenv/pyenv#installation

  2. 安装 tox-pyenv from https://pypi.org/project/tox-pyenv/

  3. 使用 pyenv , install the Python versions you want to test on

  4. 创建 .python-version file in the root of the repo, listing the Python interpreter versions you want to make available to tox (see pyenv’s documentation about this file)

    Tip: The 1st line of .python-version indicates your preferred Python version which will be used to run tox.

  5. Invoke tox with the option --tox-pyenv-no-fallback (see tox-pyenv’s documentation about this option)

housekeep.py

If you ever need to ‘reset’ your repo, you can use the housekeep.py utility like so:

$ python housekeep.py superclean
					

It is strongly recommended to NOT do superclean too often, though. Every time you invoke superclean , tox will have to recreate all its testenvs, and this will make testing much longer to finish.

superclean is typically only needed when you switch branches, or if you want to really ensure that artifacts from previous testing sessions won’t interfere with your next testing sessions.

For example, you want to force Sphinx to rebuild all documentation. Or, you’re sharing a repo between environments (say, PSCore and Cygwin) and the cached Python bytecode messes up execution (e.g., sharing the exact same directory between Windows PowerShell and Cygwin will cause problems as Python becomes confused about the locations of the source code).

签名密钥

Starting version 1.3.1, files provided through PyPI or GitHub Releases will be signed using one of the following GPG Keys:

GPG Key ID Owner Email
5D60 CE28 9CD7 C258 Pandu E POLUAN pepoluan at gmail period com

许可

aiosmtpd 在 Apache 许可 2.0 版下发行。

内容

索引和表格