我发现这个很棒的一个衬里用python创建了smtp服务器:
python -m smtpd -n -c DebuggingServer localhost:1025
麻烦的是,我的应用程序发送邮件内容base64编码,所以输出看起来真的像垃圾:
---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/alternative; boundary="===============7226846835346479139=="'
b'MIME-Version: 1.0'
b'Subject: redacted subject'
b'From: ACME Online 2'
b'To: john.doe@acme.com'
b'Cc: '
b'X-Peer: 10.255.13.37'
b''
b'--===============7226846835346479139=='
b'Content-Type: text/html; charset="utf-8"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: base64'
b''
b'PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv'
b'L0VOIj4KPGh0bWw+CjxoZWFkPgogICAgPG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBj'
b'b250ZW50PSJ0ZXh0L2h0bWw7Y2hhcnNldD1pc28tODg1OS0xIj4KICAgIDx0aXRsZT5XZWVrbHkg'
等等。
如果我知道这个,我得到:
atob('PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv')
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/"
如何获得干净的base64输出解码日志? Ps:之后我不想处理日志,我想直接输出干净的输出。它在linux docker容器中运行。
答案 0 :(得分:0)
感谢@zwer的回答,我自己实现了一个服务器:
import asyncore
import email
from smtpd import DebuggingServer
class ActualDebuggingServer(DebuggingServer):
def _print_message_content(self, peer, data):
msg = email.message_from_string(data)
for (header,value) in msg.items():
print(header, ': ', value)
for part in msg.walk():
print('---------- a part: ----------')
maybe_decoded_payload = part.get_payload(decode=True)
if (maybe_decoded_payload is not None):
print(bytes.decode(maybe_decoded_payload, encoding="utf-8"))
if __name__ == '__main__':
ActualDebuggingServer(
('0.0.0.0', 1025),
('0.0.0.0', 1025)
)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
它非常粗糙,它假设部件以UTF-8编码。另外,我不确定第二个参数是什么。请谨慎使用。