首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有基本身份验证的python aiosmtpd服务器

具有基本身份验证的python aiosmtpd服务器
EN

Stack Overflow用户
提问于 2021-02-12 11:21:46
回答 1查看 779关注 0票数 3

我试图创建一个aiosmtpd服务器来处理接收到的电子邮件。它在没有身份验证的情况下工作得很好,但我只是不知道如何设置身份验证。我已经翻阅了这些文件,并寻找了关于这方面的例子。

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

代码语言:javascript
复制
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?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-16 15:42:56

好的,因为您使用的是1.3.0版本,所以您可以按照认证文档进行操作。

一种快速的开始方法是在验证器回调指南之后创建一个“身份验证函数”(可以是处理程序类中的一个方法,也可以是独立的)。

一个简单的例子:

代码语言:javascript
复制
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)

然后创建控制器,如下所示:

代码语言:javascript
复制
    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
    )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66170956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档