python: <type 'exceptions.UnicodeEncodeError'> Why this happen?
<p>Below is the full log, the problem is in </p>

<pre><code>   23             dd.append(str(msg.get_json()))
</code></pre>

<p>I have some utf-8 returned from msg.get_json()...
Will str() try to encode the parameter using ASCII?</p>

<pre><code>&lt;type 'exceptions.UnicodeEncodeError'&gt;  Python 2.6.5: /usr/bin/python
Mon Nov 15 18:53:39 2010

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
 /usr/lib/pymodules/python2.6/flup/server/fcgi_base.py in run(self=&lt;flup.server.fcgi_base.Request object&gt;)
  556         """Runs the handler, flushes the streams, and ends the request."""
  557         try:
  558             protocolStatus, appStatus = self.server.handler(self)
  559         except:
  560             traceback.print_exc(file=self.stderr)
protocolStatus undefined, appStatus undefined, self = &lt;flup.server.fcgi_base.Request object&gt;, self.server = &lt;flup.server.fcgi.WSGIServer object&gt;, self.server.handler = &lt;bound method WSGIServer.handler of &lt;flup.server.fcgi.WSGIServer object&gt;&gt;
 /usr/lib/pymodules/python2.6/flup/server/fcgi_base.py in handler(self=&lt;flup.server.fcgi.WSGIServer object&gt;, req=&lt;flup.server.fcgi_base.Request object&gt;)
 1116         try:
 1117             try:
 1118                 result = self.application(environ, start_response)
 1119                 try:
 1120                     for data in result:
result = None, self = &lt;flup.server.fcgi.WSGIServer object&gt;, self.application = &lt;function app&gt;, 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 = &lt;function start_response&gt;
 /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=&lt;function start_response&gt;)
  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&amp;room=room2&amp;connectionid=90125080-f0a6-11df-9dcb-0800279f1ca2', global cmd_map = {'chat_message': &lt;function chat_message_cmd&gt;, 'enter_room': &lt;function enter_room_cmd&gt;, 'get_room_updates': &lt;function get_room_updates_cmd&gt;, 'login_user': &lt;function login_user_cmd&gt;, 'register_user': &lt;function register_user_cmd&gt;}, 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 = &lt;ChatConnection.ChatConnection instance&gt;, conn.get_pending_message = &lt;bound method ChatConnection.get_pending_message of &lt;ChatConnection.ChatConnection instance&gt;&gt;
 /root/bc/trunk/python/ChatConnection.py in get_pending_message(self=&lt;ChatConnection.ChatConnection instance&gt;)
   21         for 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 = &lt;built-in method append of list object&gt;, builtin str = &lt;type 'str'&gt;, msg = &lt;Message.Message instance&gt;, msg.get_json = &lt;bound method Message.get_json of &lt;Message.Message instance&gt;&gt;

&lt;type 'exceptions.UnicodeEncodeError'&gt;: '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 
</code></pre>
