gpt4 book ai didi

python - InboundEmailMessage 引发 UnicodeDecodeError 异常

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:29 27 4
gpt4 key购买 nike

在我的生产日志中,我看到许多如下错误

0.1.0.20 - - [04/Oct/2013:11:07:03 -0700] "POST /_ah/mail/mail@myapp.appspotmail.com HTTP/1.1" 500 0 - - "myapp.appspot.com" ms=258 cpu_ms=27 app_engine_release=1.8.5 instance=00c61b117c920d68f9913bdef33c5b25f4288840

E 2013-10-04 22:07:03.434
'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/webapp/mail_handlers.py", line 69, 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 775, 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 1337, 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 1246, 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 1126, 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 591, in _decode_and_join_header
for s, c in email.header.decode_header(header))
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 591, in <genexpr>
for s, c in email.header.decode_header(header))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

我怎样才能获得更多详细信息?我该如何正确处理这个异常?

最佳答案

此代码没有任何 unicode 问题(没有 InboundMailHandler):

message = mail.InboundEmailMessage(request.body)
msg_body = message.subject

编辑:

我在 dev_appserver > Inbound Mail 中进行了一些测试,主题字段中包含 "àèé" 并且主题是 unicode,但如果您的 msg_body 不是 unicode,请使用 type(msg_body) 找到编解码器并首先解码主题。

message = mail.InboundEmailMessage(request.body)
msg_body = message.subject # if unicode or
msg_body = message.subject.decode("ascii") # if ascii then
_str = msg_body.encode("utf-8") # str

关于python - InboundEmailMessage 引发 UnicodeDecodeError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19188152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com