Tornado 非阻塞 SMTP 客户端

发布于 2024-11-18 14:03:17 字数 234 浏览 0 评论 0原文

我正在寻找 python 异步 SMTP 客户端以将其与 Torando IoLoop 连接。我发现只有简单的实现(http://tornadogists.org/907491/),但它是一个阻塞解决方案,所以它可能会带来性能问题。

有人遇到过 Tornado 的非阻塞 SMTP 客户端吗?一些代码片段也非常有用。

I'am looking for python async SMTP client to connect it with Torando IoLoop. I found only simple implmementation (http://tornadogists.org/907491/) but it's a blocking solution so it might bring performance issues.

Does anyone encountered non blocking SMTP client for Tornado? Some code snippet would be also very useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫南 2024-11-25 14:03:17

仅供参考 - 我刚刚创建了一个基于 ioloop 的 smtp 客户端。虽然我不能说它已经过生产测试,但它将在不久的将来进行。

https://gist.github.com/1358253

Just FYI - I just whipped up a ioloop based smtp client. While I can't say it's production tested, it will be in the near future.

https://gist.github.com/1358253

手心的温暖 2024-11-25 14:03:17

https://github.com/equeny/tornadomail - 这是我移植 django 邮件系统和 python 的尝试smtplib 到龙卷风 ioloop。
将很高兴听到一些反馈。

https://github.com/equeny/tornadomail - here is my attemp to port django mail system and python smtplib to tornado ioloop.
Will be happy to hear some feedback.

冬天旳寂寞 2024-11-25 14:03:17

我在工作中寻找同样问题的解决方案。由于没有现成的解决方案,我将Python smtplib移植到基于tornado非阻塞IOStream的实现上。语法尽可能接近 smtplib 的语法。

# create SMTP client 
s = SMTPAsync()
yield s.connect('your.email.host',587)
yield s.starttls() 
yield s.login('username', 'password') 
yield s.sendmail('from_addr', 'to_addr', 'msg')

目前仅支持Python 3.3及以上版本。这是 github 存储库

I was looking for the solution to the same problem at work. Since there was no readily available solution, I ported Python smtplib to implementation based on tornado non-blocking IOStream. The syntax follows that of smtplib as close as possible.

# create SMTP client 
s = SMTPAsync()
yield s.connect('your.email.host',587)
yield s.starttls() 
yield s.login('username', 'password') 
yield s.sendmail('from_addr', 'to_addr', 'msg')

It currently only supports Python 3.3 and above. Here's the github repo

贵在坚持 2024-11-25 14:03:17

我编写了基于线程和队列的解决方案。每个龙卷风进程一个线程。该线程是一个工作线程,从队列中获取电子邮件,然后通过 SMTP 发送。您可以通过将龙卷风应用程序添加到队列来发送电子邮件。简单又容易。

以下是 GitHub 上的示例代码:链接

I wrote solution based on threads and queue. One thread per tornado process. This thread is a worker, gets email from queue and then send it via SMTP. You send emails from tornado application by adding it to queue. Simple and easy.

Here is sample code on GitHub: link

撕心裂肺的伤痛 2024-11-25 14:03:17

我没有使用自己的 SMTP 服务器,但认为这对某人有用:

我只需将电子邮件发送添加到我的应用程序。大多数用于网络电子邮件服务的示例 python 代码都使用阻塞设计,因此我不想使用它。

Mailchimp 的 Mandrill 使用 HTTP POST 请求,因此它可以以符合 Tornado 设计的异步方式工作。

class EmailMeHandler(BaseHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http_client = AsyncHTTPClient()
        mail_url = self.settings["mandrill_url"] + "/messages/send.json"
        mail_data = {
            "key": self.settings["mandrill_key"],
            "message": {
                "html": "html email from tornado sample app <b>bold</b>", 
                "text": "plain text email from tornado sample app", 
                "subject": "from tornado sample app", 
                "from_email": "[email protected]", 
                "from_name": "Hello Team", 
                "to":[{"email": "[email protected]"}]
            }
        }

        body = tornado.escape.json_encode(mail_data)
        response = yield tornado.gen.Task(http_client.fetch, mail_url, method='POST', body=body)
        logging.info(response)
        logging.info(response.body)

        if response.code == 200:
            self.redirect('/?notification=sent')
        else:
            self.redirect('/?notification=FAIL')

I'm not using my own SMTP server but figured this would be useful to someone:

I've just had to add email sending to my app. Most of the sample python code for the web emailing services use a blocking design so I dont want to use it.

Mailchimp's Mandrill uses HTTP POST requests so it can work in an Async fashion fitting in with Tornado's design.

class EmailMeHandler(BaseHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http_client = AsyncHTTPClient()
        mail_url = self.settings["mandrill_url"] + "/messages/send.json"
        mail_data = {
            "key": self.settings["mandrill_key"],
            "message": {
                "html": "html email from tornado sample app <b>bold</b>", 
                "text": "plain text email from tornado sample app", 
                "subject": "from tornado sample app", 
                "from_email": "[email protected]", 
                "from_name": "Hello Team", 
                "to":[{"email": "[email protected]"}]
            }
        }

        body = tornado.escape.json_encode(mail_data)
        response = yield tornado.gen.Task(http_client.fetch, mail_url, method='POST', body=body)
        logging.info(response)
        logging.info(response.body)

        if response.code == 200:
            self.redirect('/?notification=sent')
        else:
            self.redirect('/?notification=FAIL')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文