python: <type 'exceptions.UnicodeEncodeError'> Why this happen? -
below full log, problem in
23 dd.append(str(msg.get_json()))
i have utf-8 returned msg.get_json()... str() try encode parameter using ascii?
<type 'exceptions.unicodeencodeerror'> python 2.6.5: /usr/bin/python mon nov 15 18:53:39 2010 problem occurred in python script. here sequence of function calls leading error, in order occurred. /usr/lib/pymodules/python2.6/flup/server/fcgi_base.py in run(self=<flup.server.fcgi_base.request object>) 556 """runs handler, flushes streams, , ends request.""" 557 try: 558 protocolstatus, appstatus = self.server.handler(self) 559 except: 560 traceback.print_exc(file=self.stderr) protocolstatus undefined, appstatus undefined, self = <flup.server.fcgi_base.request object>, self.server = <flup.server.fcgi.wsgiserver object>, self.server.handler = <bound method wsgiserver.handler of <flup.server.fcgi.wsgiserver object>> /usr/lib/pymodules/python2.6/flup/server/fcgi_base.py in handler(self=<flup.server.fcgi.wsgiserver object>, req=<flup.server.fcgi_base.request object>) 1116 try: 1117 try: 1118 result = self.application(environ, start_response) 1119 try: 1120 data in result: result = none, self = <flup.server.fcgi.wsgiserver object>, self.application = <function app>, environ = {'content_length': '81', 'content_type': 'application/x-www-form-urlencoded; charset=utf-8', 'document_root': '/usr/local/nginx/html', 'document_uri': '/a.bc', 'gateway_interface': 'cgi/1.1', 'http_accept': 'application/json, text/javascript, */*', 'http_accept_charset': 'gb2312,utf-8;q=0.7,*;q=0.7', 'http_accept_encoding': 'gzip,deflate', 'http_accept_language': 'zh-cn,zh;q=0.5', 'http_cache_control': 'no-cache', ...}, start_response = <function start_response> /root/bc/trunk/python/server.py in app(environ={'content_length': '81', 'content_type': 'application/x-www-form-urlencoded; charset=utf-8', 'document_root': '/usr/local/nginx/html', 'document_uri': '/a.bc', 'gateway_interface': 'cgi/1.1', 'http_accept': 'application/json, text/javascript, */*', 'http_accept_charset': 'gb2312,utf-8;q=0.7,*;q=0.7', 'http_accept_encoding': 'gzip,deflate', 'http_accept_language': 'zh-cn,zh;q=0.5', 'http_cache_control': 'no-cache', ...}, start_response=<function start_response>) 125 config.dbg(cmd_name) 126 if cmd_name in cmd_map: 127 ret = cmd_map[cmd_name](q) 128 else: 129 ret = '{"' + cmd_name + '": {"result":"error"}}'; ret = 'cmd=get_room_updates&room=room2&connectionid=90125080-f0a6-11df-9dcb-0800279f1ca2', global cmd_map = {'chat_message': <function chat_message_cmd>, 'enter_room': <function enter_room_cmd>, 'get_room_updates': <function get_room_updates_cmd>, 'login_user': <function login_user_cmd>, 'register_user': <function register_user_cmd>}, cmd_name = 'get_room_updates', q = {'cmd': ['get_room_updates'], 'connectionid': ['90125080-f0a6-11df-9dcb-0800279f1ca2'], 'room': ['room2']} /root/bc/trunk/python/server.py in get_room_updates_cmd(qs={'cmd': ['get_room_updates'], 'connectionid': ['90125080-f0a6-11df-9dcb-0800279f1ca2'], 'room': ['room2']}) 88 room = config.gonlinerooms[room_name] 89 conn = room.chat_connections[connectionid] 90 remaining = conn.get_pending_message() 91 92 return remaining remaining = '[]', conn = <chatconnection.chatconnection instance>, conn.get_pending_message = <bound method chatconnection.get_pending_message of <chatconnection.chatconnection instance>> /root/bc/trunk/python/chatconnection.py in get_pending_message(self=<chatconnection.chatconnection instance>) 21 msg in self.pending_message: 22 txt = txt + msg.get_json() + "," 23 dd.append(str(msg.get_json())) 24 25 config.dbg("dd = " + str(dd)) dd = [], dd.append = <built-in method append of list object>, builtin str = <type 'str'>, msg = <message.message instance>, msg.get_json = <bound method message.get_json of <message.message instance>> <type 'exceptions.unicodeencodeerror'>: 'ascii' codec can't encode characters in position 79-80: ordinal not in range(128) args = ('ascii', u'{"id":"168", "username":"binc2", "ctime":"2010-11-15 18:53:37.165260", "body":"\u4e2d\u56fd", "room":"room2"}', 79, 81, 'ordinal not in range(128)') encoding = 'ascii' end = 81 message = '' object = u'{"id":"168", "username":"binc2", "ctime":"2010-11-15 18:53:37.165260", "body":"\u4e2d\u56fd", "room":"room2"}' reason = 'ordinal not in range(128)' start = 79
the problem ocurring in str(msg.get_json())
or 1 of other places use str
. object contains unicode data , str
not built handle unicode. should able use json.dumps(msg.get_json())
instead. alternatively use str(msg.get_json().decode('utf-8'))
.
Comments
Post a Comment