【问题标题】:python aiosmtpd server with basic authentication具有基本身份验证的 python aiosmtpd 服务器
【发布时间】:2021-02-16 23:42:56
【问题描述】:

我正在尝试创建一个 aiosmtpd 服务器来处理收到的电子邮件。 它在没有身份验证的情况下工作得很好,但我根本不知道如何设置身份验证。 我浏览了文档并搜索了这方面的示例。

我目前如何使用它的示例:

from aiosmtpd.controller import Controller

class CustomHandler:
    async def handle_DATA(self, server, session, envelope):
        peer = session.peer
        mail_from = envelope.mail_from
        rcpt_tos = envelope.rcpt_tos
        data = envelope.content         # type: bytes
        # Process message data...
        print('peer:' + str(peer))
        print('mail_from:' + str(mail_from))
        print('rcpt_tos:' + str(rcpt_tos))
        print('data:' + str(data))
        return '250 OK'

if __name__ == '__main__':
    handler = CustomHandler()
    controller = Controller(handler, hostname='192.168.8.125', port=10025)
    # Run the event loop in a separate thread.
    controller.start()
    # Wait for the user to press Return.
    input('SMTP server running. Press Return to stop server and exit.')
    controller.stop()```

which is the basic method from the documentation.

could someone please provide me with an example as to how to do simple authentication?

【问题讨论】:

  • 嗨,我是aiosmtpd 的维护者之一。你用的是什么版本的aiosmtpd
  • 您好,我使用的是 1.3.0 版

标签: python aiosmtpd


【解决方案1】:

好的,由于您使用的是1.3.0版本,您可以关注documentation for Authentication

快速开始的方法是创建一个遵循Authenticator Callback guidelines 的“身份验证器函数”(可以是处理程序类中的方法,也可以是独立的)。

一个简单的例子:

from aiosmtpd.smtp import AuthResult, LoginPassword

auth_db = {
    b"user1": b"password1",
    b"user2": b"password2",
    b"user3": b"password3",
}

# Name can actually be anything
def authenticator_func(server, session, envelope, mechanism, auth_data):
    # For this simple example, we'll ignore other parameters
    assert isinstance(auth_data, LoginPassword)
    username = auth_data.login
    password = auth_data.password
    # If we're using a set containing tuples of (username, password),
    # we can simply use `auth_data in auth_set`.
    # Or you can get fancy and use a full-fledged database to perform
    # a query :-)
    if auth_db.get(username) == password:
        return AuthResult(success=True)
    else:
        return AuthResult(success=False, handled=False)

然后你正在创建控制器,像这样创建它:

    controller = Controller(
        handler,
        hostname='192.168.8.125',
        port=10025,
        authenticator=authenticator_func,  # i.e., the name of your authenticator function
        auth_required=True,  # Depending on your needs
    )

【讨论】:

  • 这正是我想要的。今晚我会试一试,非常感谢。
  • 这个认证功能对我不起作用。当我尝试访问 SMTP 服务器时,我收到一条错误消息,提示服务器不支持 AUTH。
  • @MattieG4 您很可能在未指定 TLS 上下文的情况下启动控制器。在这种情况下,您需要将 auth_require_tls=False 传递给 SMTP 构造函数。如果您使用的是最新版本,只需在调用 Controller() 时添加 auth_require_tls=False 作为最后一个 kwarg
  • @MattieG4 如果您认为服务器行为异常,请检查服务器日志。最新版本的 aiosmtpd 具有相当广泛的日志记录以帮助进行故障排除。默认情况下,它会记录到 syslog 的 mail 设施。
  • 可以更新示例以包含所需的额外参数吗?
猜你喜欢
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多