2017-08-31 67 views
2

我正在使用「標準」入站郵件處理程序,接收示例in the docs的入站電子郵件到我的Google App Engine應用程序。Google App Engine python入站郵件LookupError:未知編碼

似乎發送給應用程序(不是我)的某個電子郵件消息正在導致電子郵件API發出LookupError:未知編碼異常。請參閱回溯。

據我所知,這個異常在應用程序的郵件處理程序被調用之前被拋出,顯然使得它不可能被應用程序的代碼捕獲和處理。這是真的嗎?

似乎App Engine以約40分鐘的時間間隔重試失敗的消息,這會繼續產生錯誤(並且會發出警報...)有什麼方法可以中止嗎?

感謝您的幫助。

回溯:

Traceback (most recent call last): 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 70, in post 
    self.receive(mail.InboundEmailMessage(self.request.body)) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 939, in __init__ 
    self.update_from_mime_message(mime_message) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1513, in update_from_mime_message 
    super(InboundEmailMessage, self).update_from_mime_message(mime_message) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1422, in update_from_mime_message 
    super(EmailMessage, self).update_from_mime_message(mime_message) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1291, in update_from_mime_message 
    subject = _decode_and_join_header(mime_message['subject'], separator=u'') 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 597, in _decode_and_join_header 
    for s, charset in email.header.decode_header(header)) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 597, in 
    for s, charset in email.header.decode_header(header)) 
LookupError: unknown encoding: iso-8859-8-i

回答

4

當入站郵件處理的post方法被調用的錯誤發生。

File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 70, in post 
    self.receive(mail.InboundEmailMessage(self.request.body)) 

最簡單的辦法是重寫post方法在自己的處理程序捕獲錯誤:

import logging 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 


class MyInboundMailHandler(InboundMailHandler): 

    def post(self): 
     try: 
      super(MyInboundMailHandler, self).post() 
     except LookupError as ex: 
      logging.warning('Could not process message because %s.', ex) 

    def receive(self, mail_message): 
     # Process message 

如果你不想失去消息,您可以創建並註冊自定義iso-8859-8-i編解碼器。這似乎不是一個證據充分的過程,但這些問題提供了一些線索:

How do I properly create custom text codecs?

Custom Python Charmap Codec

how do I write a custom encoding in python to clean up my data?

標準庫的iso-8859-8 encoding提供了一個很好的範本。

+0

非常感謝您,作爲第一項措施,我會按照您的建議覆蓋InboundMailHandler發佈方法。 看起來iso-8859-8和iso-8859-8-i只在幾個控制代碼中有所不同,對於大多數應用來說這可能沒有意義。有沒有簡單的方法來強制查詢iso-8859-8編解碼器,無論何時請求iso-8859-8-i編解碼器,而無需實際註冊整個新編解碼器? – ACEGL

+0

顯然有https://stackoverflow.com/a/1064308/5320906 – snakecharmerb

+0

太好了。再次感謝。 – ACEGL