奇怪的GAE Python编码问题
我正在尝试从Twitter获取tweet,并使用GAE中的xmppapi通过GTalk发送。我有一个很奇怪的问题 我已成功地从Twitter检索到数据。当我使用XMPP API发送它们时会发生错误。根据跟踪信息,错误发生在GAE代码中,而不是我自己的代码中。我已经用Python2.5测试了特定的tweet,它能够正确处理tweet文本,并且tweet只包含英语字符奇怪的GAE Python编码问题,python,google-app-engine,character-encoding,Python,Google App Engine,Character Encoding,我正在尝试从Twitter获取tweet,并使用GAE中的xmppapi通过GTalk发送。我有一个很奇怪的问题 我已成功地从Twitter检索到数据。当我使用XMPP API发送它们时会发生错误。根据跟踪信息,错误发生在GAE代码中,而不是我自己的代码中。我已经用Python2.5测试了特定的tweet,它能够正确处理tweet文本,并且tweet只包含英语字符 ERROR 2011-12-08 14:29:54,200 dev_appserver.py:2700] Exception
ERROR 2011-12-08 14:29:54,200 dev_appserver.py:2700] Exception encountered handling request
Traceback (most recent call last):
File "/home/google_appengine/google/appengine/tools/dev_appserver.py", line 2641, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "/home/google_appengine/google/appengine/tools/dev_appserver.py", line 2528, in _Dispatch
base_env_dict=env_dict)
File "/home/google_appengine/google/appengine/tools/dev_appserver.py", line 616, in Dispatch
base_env_dict=base_env_dict)
File "/home/google_appengine/google/appengine/tools/dev_appserver.py", line 1592, in Dispatch
self._module_dict)
File "/home/google_appengine/google/appengine/tools/dev_appserver.py", line 1517, in ExecuteCGI
logservice_stub._flush_logs_buffer()
File "/home/google_appengine/google/appengine/api/logservice/logservice_stub.py", line 71, in _flush_logs_buffer
logservice.logs_buffer().flush()
File "/home/google_appengine/google/appengine/api/logservice/logservice.py", line 228, in flush
self._lock_and_call(self._flush)
File "/home/google_appengine/google/appengine/api/logservice/logservice.py", line 112, in _lock_and_call
return method(*args)
File "/home/google_appengine/google/appengine/api/logservice/logservice.py", line 260, in _flush
apiproxy_stub_map.MakeSyncCall('logservice', 'Flush', request, response)
File "/home/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 94, in MakeSyncCall
return stubmap.MakeSyncCall(service, call, request, response)
File "/home/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 308, in MakeSyncCall
rpc.CheckSuccess()
File "/home/google_appengine/google/appengine/api/apiproxy_rpc.py", line 156, in _WaitImpl
self.request, self.response)
File "/home/google_appengine/google/appengine/api/apiproxy_stub.py", line 87, in MakeSyncCall
method(request, response)
File "/home/google_appengine/google/appengine/api/logservice/logservice_stub.py", line 309, in _Dynamic_Flush
new_app_logs = self.put_log_lines(group.log_line_list())
File "/home/google_appengine/google/appengine/api/logservice/logservice_stub.py", line 321, in put_log_lines
return _run_in_namespace(self._put_log_lines, lines)
File "/home/google_appengine/google/appengine/api/logservice/logservice_stub.py", line 93, in _run_in_namespace
return f(*args)
File "/home/google_appengine/google/appengine/api/logservice/logservice_stub.py", line 330, in _put_log_lines
message=app_log.message())
File "/home/google_appengine/google/appengine/ext/db/__init__.py", line 945, in __init__
prop.__set__(self, value)
File "/home/google_appengine/google/appengine/ext/db/__init__.py", line 599, in __set__
value = self.validate(value)
File "/home/google_appengine/google/appengine/ext/db/__init__.py", line 2696, in validate
value = self.data_type(value)
File "/home/google_appengine/google/appengine/api/datastore_types.py", line 1138, in __new__
return super(Text, cls).__new__(cls, arg, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xca in position 135: ordinal not in range(128)
此错误表示您试图将多字节字符存储在7位ASCII字符串中。将HTTP响应正文作为unicode传递以避免这种情况。如果您需要更具体的建议,请发布您的代码。对不起,伙计们……假警报:( 问题的原因是Google日志服务只接受'ascii'编解码器,当我在本地测试XMPP服务时,它使用日志服务显示消息,而我发送的消息是Unicode格式的,因此发生错误
解决方案是使用str.encode('ascii','ignore')测试时,或者干脆忽略该错误。'ascii'编解码器无法解码字节0xca稍微偏离主题:twitter和GAE afaik存在问题。twitter按域进行节流-如果您在appspot上运行,您与在appspot上与twitter对话的其他应用程序共享有限的资源-我们发现这很困难ult需要处理。不确定情况是否仍然如此。它们通过IP而不是域进行节流,尽管效果是一样的:应用程序引擎请求汇集在一起,并被视为一个非常繁忙的应用程序。如果这是问题,你可能会得到一个下载错误,而不是UnicodeDecodeError。我的错误-道歉-并没有表明这是问题的根源错误,只是强调了在GAE上实现twitter应用程序有问题的一点。根据堆栈跟踪,在我看来,您发出的请求包含非ASCII编码的字符;当写入日志条目时,数据库会引发异常。随机思考:您是在添加具有未编码路径的任务?还是正在使用在dev_appserver上使用XMPP模拟器发布未编码的数据?它不是“7位ASCII字符串”或“多字节字符”。这意味着他们试图使用ASCII解码字符串,但该字符串包含ASCII范围以外的代码点。