首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3SMTP aiosmtpd代理/中继

Python3SMTP aiosmtpd代理/中继
EN

Stack Overflow用户
提问于 2017-04-07 13:07:34
回答 1查看 3K关注 0票数 0

我正在尝试使用替换aiosmtpd的新的smtpd库来进行一个开放的SMTP中继。下面的程序实例化传递给邮件控制器的Proxy处理程序,邮件控制器随后在后台启动。然后使用连接到中继的标准smtplib客户端创建消息。

在客户端和中继之间的SMTP会话结束之前,一切都是正常的,直到消息从不离开中继为止。中继从不用250 OK\r\n进行应答,而ctrl+c则表示sendmail正在等待回复。

有什么想法吗?剧本漏掉了什么吗?

编辑mail.example.com只是一个示例服务器。smtpd DebuggingServer在使用relay = aiosmtpd.handlers.Proxy("localhost", 1025)执行脚本时不会打印任何内容。

代码语言:javascript
复制
$ python3.6 -m smtpd -n -c DebuggingServer -d localhost:1025
DebuggingServer started at Fri Apr  7 18:41:09 2017
    Local addr: ('localhost', 1025)
    Remote addr:('localhost', 25)
...nothing printed out...

剧本:

代码语言:javascript
复制
from aiosmtpd.handlers import Debugging, Proxy
from aiosmtpd.controller import Controller
from smtplib import SMTP

# relay = aiosmtpd.handlers.Debugging()
relay = aiosmtpd.handlers.Proxy("localhost", 1025)
# relay = aiosmtpd.handlers.Proxy("mail.example.com", 25)
controller = Controller(relay)
controller.start()
print(controller, controller.hostname, controller.port)

input("ready... press enter to continue")

print("creating SMTP with debug")
client = SMTP()
client.set_debuglevel(1)
print("connecting to the SMTP server")
client.connect(controller.hostname, controller.port)

print("sending message")
client.sendmail('alice@example.com',
                ['bob@example.com'], """\
From: Alice <alice@example.com>
To: Bob <bob@example.com>
Subject: Title

Body.
""")

print("stopping controller")
controller.stop()
print("checking if controller really stopped")
client.connect(controller.hostname, controller.port)

下面是脚本的输出:

代码语言:javascript
复制
 $ python3.6 relay.py
<aiosmtpd.controller.Controller object at 0x10199f710> ::0 8025
ready... press enter to continue
creating SMTP with debug
connecting to the SMTP server
connect: ('::0', 8025)
connect: to ('::0', 8025) None
reply: b'220 localhost Python SMTP 1.0a4\r\n'
reply: retcode (220); Msg: b'localhost Python SMTP 1.0a4'
connect: b'localhost Python SMTP 1.0a4'
sending message
send: 'ehlo localhost\r\n'
reply: b'250-localhost\r\n'
reply: b'250-SIZE 33554432\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250 HELP\r\n'
reply: retcode (250); Msg: b'localhost\nSIZE 33554432\n8BITMIME\nHELP'
send: 'mail FROM:<alice@example.com> size=85\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<bob@example.com>\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
send: 'data\r\n'
reply: b'354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'From: Alice <alice@example.com>\r\nTo: Bob <bob@example.com>\r\nSubject: Title\r\n\r\nBody.\r\n.\r\n'

...nothing happens at this point...

^CTraceback (most recent call last):
  File "relay.py", line 49, in <module>
    """)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 881, in sendmail
    (code, resp) = self.data(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 568, in data
    (code, msg) = self.getreply()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 386, in getreply
    line = self.file.readline(_MAXLINE + 1)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
KeyboardInterrupt

为了进行比较,下面是使用调试处理程序的输出:

代码语言:javascript
复制
 $ python3.6 relay.py
<aiosmtpd.controller.Controller object at 0x10189f710> ::0 8025
ready... press enter to continue
creating SMTP with debug
connecting to the SMTP server
connect: ('::0', 8025)
connect: to ('::0', 8025) None
reply: b'220 localhost Python SMTP 1.0a4\r\n'
reply: retcode (220); Msg: b'localhost Python SMTP 1.0a4'
connect: b'localhost Python SMTP 1.0a4'
sending message
send: 'ehlo localhost\r\n'
reply: b'250-localhost\r\n'
reply: b'250-SIZE 33554432\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250 HELP\r\n'
reply: retcode (250); Msg: b'localhost\nSIZE 33554432\n8BITMIME\nHELP'
send: 'mail FROM:<alice@example.com> size=85\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<bob@example.com>\r\n'
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
send: 'data\r\n'
reply: b'354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'From: Alice <alice@example.com>\r\nTo: Bob <bob@example.com>\r\nSubject: Title\r\n\r\nBody.\r\n.\r\n'
---------- MESSAGE FOLLOWS ----------
mail options: ['SIZE=85']
rcpt options: []

From: Alice <alice@example.com>
To: Bob <bob@example.com>
Subject: Title
X-Peer: ('::1', 64397, 0, 0)

Body.
------------ END MESSAGE ------------
reply: b'250 OK\r\n'
reply: retcode (250); Msg: b'OK'
data: (250, b'OK')
stopping controller
checking if controller really stopped
connect: ('::0', 8025)
connect: to ('::0', 8025) None
Traceback (most recent call last):
  File "relay.py", line 51, in <module>
    client.connect(controller.hostname, controller.port)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 335, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 306, in _get_socket
    self.source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 722, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-26 15:53:49

编辑:在aiosmtpd 1.0b1中,bug是固定,所以升级应该解决这个问题。

在aiosmtpd 1.0a4中,Proxy.handle_DATA中的一个未察觉的异常(使用data作为str而不是字节)将导致异步任务停止,但异常永远不会被传播。

如果升级到1.0a5,则会正确地打印异常:"Error:(TypeError)不能在类似字节的对象上使用字符串模式“。问题是,从1.0a5开始,aiosmtpd中的Proxy被设计用于在aiosmtpd.smtp.SMTP对象上设置decode_data=True的Controller子类中使用,但是默认情况下,会导致错误的decode_data=False。(在我看来,Proxy应该改为使用decode_data=False,因此我打开了PR #74。)

因此,要在aiosmtpd1.0a4或1.0a5中使用代理,需要从UTF8Controller中复制和使用aiosmtpd/tests/test_handlers.py子类。我已经用1.0a4和1.0a5测试了以下脚本:

代码语言:javascript
复制
from aiosmtpd.handlers import Debugging, Proxy
from aiosmtpd.controller import Controller
from aiosmtpd.smtp import SMTP as SMTPServer
from smtplib import SMTP as SMTPClient

class UTF8Controller(Controller):
    def factory(self):
        return SMTPServer(self.handler, decode_data=True)

# relay = Debugging()
relay = Proxy("localhost", 1025)
# relay = Proxy("mail.example.com", 25)
controller = UTF8Controller(relay)
controller.start()
print(controller, controller.hostname, controller.port)

input("ready... press enter to continue")

print("creating SMTP with debug")
client = SMTPClient()
client.set_debuglevel(1)
print("connecting to the SMTP server")
client.connect(controller.hostname, controller.port)

print("sending message")
client.sendmail('alice@example.com',
                ['bob@example.com'], """\
From: Alice <alice@example.com>
To: Bob <bob@example.com>
Subject: Title

Body.
""")

print("stopping controller")
controller.stop()
print("checking if controller really stopped")
client.connect(controller.hostname, controller.port)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43278998

复制
相关文章

相似问题

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