GAE:如何在url中使用unicode字符

时间:2013-03-20 15:55:34

标签: google-app-engine python-2.7 sqlite url-routing

我刚刚将GAE升级到1.7.6(python 2.7)。我的应用程序在URL中使用了一些西班牙语字符(例如“españa”或“cádiz”)。到目前为止,我处理它们没有任何问题。但是,升级后,我记得有错误信息和我的应用程序崩溃,每当我在我的网址中引入特殊字符时:

ProgrammingError('You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.',)
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
req.respond()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
self.server.gateway(self).respond()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 230, in __call__
return app(environ, start_response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/server.py", line 1114, in __call__
return self._handle_request(environ, start_response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/server.py", line 512, in _handle_request
http_version=http_version)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub.py", line 160, in WrappedMethod
return method(self, *args, **kwargs)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/logservice/logservice_stub.py", line 151, in start_request
host, start_time, method, resource, http_version))
ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

我写了一个简单的应用程序来调试问题,没有任何积极的结果:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

class MyHandler(webapp2.RequestHandler):
  def get(self, param):
    self.response.write(param)

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/(.*)', MyHandler)
], debug=True)

访问网址“/ hola”时,响应符合预期,浏览器会显示“hola”字样。但是,当访问“/españa”或甚至“/ espa%C3%B1a”时,开发服务器崩溃并显示上面显示的错误。

请注意,错误只发生在开发中,我猜它与新的sqlite数据库有关;但是,在前面的例子中,我根本没有使用数据库!

有什么想法吗?请帮助!

1 个答案:

答案 0 :(得分:2)

它似乎是开发服务器的错误。现在最好的办法是在GAE错误跟踪器上标记错误:GAE bug - url's with unicode characters

希望如果有更多人“加注”它,他们会更快地解决这个问题。