1

我想使用 Python 制作一个小型 SMTP 服务器进行测试,所以我正在尝试服务器示例代码

https://pymotw.com/2/smtpd/

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

def process_message(self, peer, mailfrom, rcpttos, data):
    print 'Receiving message from:', peer
    print 'Message addressed from:', mailfrom
    print 'Message addressed to  :', rcpttos
    print 'Message length        :', len(data)
    return

server = CustomSMTPServer(('127.0.0.1', 1025), None)

asyncore.loop()

连同同一页面上的示例客户端代码:

import smtplib
import email.utils
from email.mime.text import MIMEText

# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', 'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'Simple test message'

server = smtplib.SMTP('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
try:
    server.sendmail('author@example.com', ['recipient@example.com'], msg.as_string())
finally:
    server.quit()

但是,当我尝试运行客户端时,我在服务器端得到以下信息:

error: uncaptured python exception, closing channel <smtpd.SMTPChannel connected 127.0.0.1:38634 at 0x7fe28a901490> (<class 'TypeError'>:process_message() got an unexpected keyword argument 'mail_options' [/root/Python-3.8.1/Lib/asyncore.py|read|83] [/root/Python-3.8.1/Lib/asyncore.py|handle_read_event|420] [/root/Python-3.8.1/Lib/asynchat.py|handle_read|171] [/root/Python-3.8.1/Lib/smtpd.py|found_terminator|386])
^CTraceback (most recent call last):
  File "./mysmtpd.py", line 18, in <module>
asyncore.loop()
  File "/root/Python-3.8.1/Lib/asyncore.py", line 203, in loop
poll_fun(timeout, map)
  File "/root/Python-3.8.1/Lib/asyncore.py", line 144, in poll
r, w, e = select.select(r, w, e, timeout)
KeyboardInterrupt

然后我找到了这个问题页面:

https://bugs.python.org/issue35837

我认为这就是我一直遇到的问题。

该问题尚未解决,因此我想知道是否可以在示例客户端代码中修改某些内容来解决该问题中描述的问题?

谢谢,吉姆

4

3 回答 3

0

仅供参考,对 rcpt_options 参数执行相同操作。

参考:https ://docs.python.org/3/library/smtpd.html?highlight=process_message#smtpd.SMTPServer.process_message

于 2021-06-07T19:58:22.307 回答
0

mail_options=None在你的process_message()函数中添加一个。

于 2020-08-07T05:44:29.350 回答
-1

该错误似乎与def process_message(self, peer, mailfrom, rcpttos, data):. 您可以将其替换为def process_message(self, peer, mailfrom, rcpttos, data,**my_krargs):

于 2022-01-16T11:03:47.953 回答