2012-05-31 30 views
0

我正在處理來自節的信息,其中一個目標是檢查是否有錯誤並通過電子郵件發送通知。發送電子郵件與節日誌信息

我已經能夠做的就是捕捉錯誤(或者IqError或IqTimeout)和發送普通電子郵件與書面資料:

except IqTimeout: 
    USERNAME = 'user' 
    PASSWORD = 'password' 
    sender = '[email protected]' 
    receivers = '[email protected]' 
    message = """From: SomeGuy <[email protected]> 
     To: Barbara Shell <[email protected]> 
     Subject: Alert of an Error 

     This is a test e-mail message: IqTimeout 
     """ 
     smtpObj = smtplib.SMTP('smtp.sendmail.net', 123) 
     smtpObj.login(USERNAME, PASSWORD) 
     smtpObj.sendmail(sender, receivers, message)   
     print "Successfully sent email" 

我也tryed打印日誌,從信息的「錯誤」節,但只得到了一個字段:

logging.error("Could not register account. Error code: %s" %e.iq['error']['code']) 

我要的是錯誤的信息,形成我的節被髮送到電子郵件,基本上可以像這樣的東西去:

To: [email protected] 
From: [email protected] 
Subject: Alert of an Error 

This is your error: 

RECV: <iq to="[email protected]/36457242971338462713453506" from="chat.test.net" id="3" type="error"><oo xmlns="http://chat.net/profile" /><error code="404" type="cancel"><remote-server-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /></error></iq> 

我正在寫python。如果您需要更多信息,我會更願意回答!

在此先感謝!

問候,

+0

您使用的是什麼XMPP庫? – MattJ

+0

使用sleekxmpp – BarbSchael

回答

0

如果你想在電子郵件中收到的錯誤節,你只需要做到:

except IqError as e: 
    ... 
    message = """... 

    RECV: %s""" % e.iq 

如果你想提取錯誤數據的唯一部分,你可以使用任何的:

e.iq['error']['code'] 
e.iq['error']['type'] 
e.iq['error']['condition'] 
e.iq['error']['text'] 

它,在你的榜樣將分別提供:

'404' 
'cancel' 
'remote-server-not-found' 
"" # Because no human friendly text was included 

現在,這裏是您可能遇到的問題。當您發現​​異常時,e.iq是包含可以像上面那樣訪問的錯誤數據的響應節。如果您正在捕捉IqTimeout,那麼e.iq就是原始節,它從未收到回覆。在這種情況下,從XML中提取數據確實沒有錯誤。當然,你仍然可以得到看起來像錯誤數據的東西,但這可能是因爲在訪問e.iq['error']時插入了一個錯誤元素(如果它不存在的話)。

不要忘記有SleekXMPP相關幫助的[email protected]空間。

- 蘭斯

+0

感謝蘭斯!現在我怎麼輸入[email protected]?新的! – BarbSchael

+0

如果使用adium,則選擇File> Join Group Chat,然後輸入圓滑的房間名稱和conference.jabber.org作爲服務器。大多數客戶以類似的方式運作。 –

相關問題