下面的服务器几乎是直接从aiosmtpd文档获取的:
import asyncio
import ssl
from aiosmtpd.controller import Controller
class ExampleHandler:
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
if not address.endswith('@example.com'):
return '550 not relaying to that domain'
envelope.rcpt_tos.append(address)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
print(f'Message from {envelope.mail_from}')
print(f'Message for {envelope.rcpt_tos}')
print(f'Message data:\n{envelope.content.decode("utf8", errors="replace")}')
print('End of message')
return '250 Message accepted for delivery'
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
controller = Controller(ExampleHandler(), port=8026, ssl_context=context)
controller.start()
input('Press enter to stop')
controller.stop()但是,当我启动此服务器并尝试使用swaks向其发送电子邮件时:
echo 'Testing' | swaks --to wayne@example.com --from "something@example.org" --server localhost --port 8026 -tls30岁以后就会超时了。如果我从服务器上删除ssl_context=context,从客户端删除-tls,那么它就会发送邮件。
另外,当我试图通过telnet连接并发送EHLO whatever时,服务器实际上关闭了连接。
实现支持tls的aiosmtpd服务器的正确方法是什么?
发布于 2017-08-02 07:51:54
基于韦恩自己的答案,下面是如何使用aiosmtpd创建一个STARTTLS服务器。
1.创建SSL上下文
要进行测试,请使用以下命令为localhost生成一个自签名证书
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj '/CN=localhost'
使用ssl模块将其加载到Python中:
import ssl
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain('cert.pem', 'key.pem')2.将SSL上下文传递给aiosmtpd
创建aiosmtpd的Controller的子类,将此上下文作为tls_context传递给SMTP
from aiosmtpd.smtp import SMTP
from aiosmtpd.controller import Controller
class ControllerTls(Controller):
def factory(self):
return SMTP(self.handler, require_starttls=True, tls_context=context)3.运行它
使用处理程序实例化此控制器并启动它。在这里,我使用了aiosmtpd自己的Debugging处理程序:
from aiosmtpd.handlers import Debugging
controller = ControllerTls(Debugging(), port=1025)
controller.start()
input('Press enter to stop')
controller.stop()4.测试
将本地邮件客户端配置为发送到localhost:1025,或使用swaks
swaks -tls -t test --server localhost:1025
..。或者在发出初始的openssl s_client命令后,使用STARTTLS与服务器对话:
openssl s_client -crlf -CAfile cert.pem -connect localhost:1025 -starttls smtp
完整代码
下面的代码还使用swaks来测试服务器,它还展示了如何创建一个服务器(如韦恩的答案)。
import os
import ssl
import subprocess
from aiosmtpd.smtp import SMTP
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Debugging
# Create cert and key if they don't exist
if not os.path.exists('cert.pem') and not os.path.exists('key.pem'):
subprocess.call('openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem ' +
'-days 365 -nodes -subj "/CN=localhost"', shell=True)
# Load SSL context
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain('cert.pem', 'key.pem')
# Pass SSL context to aiosmtpd
class ControllerStarttls(Controller):
def factory(self):
return SMTP(self.handler, require_starttls=True, tls_context=context)
# Start server
controller = ControllerStarttls(Debugging(), port=1025)
controller.start()
# Test using swaks (if available)
subprocess.call('swaks -tls -t test --server localhost:1025', shell=True)
input('Running STARTTLS server. Press enter to stop.\n')
controller.stop()
# Alternatively: Use TLS-on-connect
controller = Controller(Debugging(), port=1025, ssl_context=context)
controller.start()
# Test using swaks (if available)
subprocess.call('swaks -tlsc -t test --server localhost:1025', shell=True)
input('Running TLSC server. Press enter to stop.\n')
controller.stop()https://stackoverflow.com/questions/45447491
复制相似问题