首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决Python smtpd的问题?

如何解决Python smtpd的问题?
EN

Stack Overflow用户
提问于 2020-04-15 00:54:38
回答 3查看 977关注 0票数 1

我想使用Python创建一个小型SMTP服务器进行测试,所以我尝试了服务器示例代码

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

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

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

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

然而,当我尝试运行客户端时,我在服务器端得到了以下结果:

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

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

这个问题还没有解决,所以我想知道,在此期间,是否可以在示例客户端代码中修改一些东西,以绕过该问题中描述的问题?

谢谢,吉姆

EN

回答 3

Stack Overflow用户

发布于 2020-08-07 13:44:29

process_message()函数中添加一个mail_options=None

票数 0
EN

Stack Overflow用户

发布于 2021-06-08 03:58:22

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

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

票数 0
EN

Stack Overflow用户

发布于 2022-01-16 11:03:48

错误似乎出现在def process_message(self, peer, mailfrom, rcpttos, data):行中。您可以将其替换为def process_message(self, peer, mailfrom, rcpttos, data,**my_krargs):

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61213049

复制
相关文章

相似问题

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