2011-07-04 123 views
3

我正在尋找python異步SMTP客戶端來連接它與Torando IoLoop。我發現只有簡單的implmementation(http://tornadogists.org/907491/),但它是一個阻塞解決方案,所以它可能會帶來性能問題。Tornado非阻塞SMTP客戶端

有沒有人遇到過非阻塞的Tornado SMTP客戶端?一些代碼片段也會非常有用。

回答

2

我寫了基於線程和隊列的解決方案。每個龍捲風過程一個線程。該線程是一名工作人員,從隊列中獲取電子郵件,然後通過SMTP發送。您通過將其添加到隊列中來發送來自龍捲風應用程序的電子郵件。簡單而簡單。

這裏是在GitHub上的示例代碼:link

+0

帶有工作線程的解決方案似乎非常優雅,而沒有異步解決方案。你有沒有與社區分享你的代碼? – berni

+0

這是一個簡單的解決方案。尚未用於生產:[鏈接](https://github.com/marcinc81/quemail) –

2

我不會用我自己的SMTP服務器,但想通這將是有用的人:

我剛剛加入電子郵件發送到我的應用程序。大多數用於Web電子郵件服務的示例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') 
3

我正在尋找工作中同樣問題的解決方案。由於沒有現成的解決方案,我將Python smtplib移植到基於龍捲風非阻塞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 repo