我想使用Python创建一个小型SMTP服务器进行测试,所以我尝试了服务器示例代码
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
我认为这就是我一直遇到的问题。
这个问题还没有解决,所以我想知道,在此期间,是否可以在示例客户端代码中修改一些东西,以绕过该问题中描述的问题?
谢谢,吉姆
发布于 2020-08-07 13:44:29
在process_message()函数中添加一个mail_options=None。
发布于 2021-06-08 03:58:22
仅供参考,对rcpt_options参数执行相同的操作。
发布于 2022-01-16 11:03:48
错误似乎出现在def process_message(self, peer, mailfrom, rcpttos, data):行中。您可以将其替换为def process_message(self, peer, mailfrom, rcpttos, data,**my_krargs):
https://stackoverflow.com/questions/61213049
复制相似问题