我的web應用程序之前跑得非常好,但日前一個問題就來了,現在我可以開始我的web應用程序,但是當我瀏覽我的從本地(127.0.0.1)站點或遠程(192.168.xxx.xxx)(僅簡單地打開網頁,無論從鼠標和鍵盤輸入),crashs這樣的web應用程序:的UnicodeDecodeError:「UTF-8」編解碼器不能在5位解碼字節0xcb:無效延續字節
Traceback (most recent call last):
File "/path/to/project/web/application.py", line 242, in process
return self.handle()
File "/path/to/project/web/application.py", line 233, in handle
return self._delegate(fn, self.fvars, args)
File "/path/to/project/web/application.py", line 415, in _delegate
return handle_class(cls)
File "/path/to/project/web/application.py", line 390, in handle_class
return tocall(*args)
File "./my_web_app.py", line 40, in GET
simplejson.dumps(manus))
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 286, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 226, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 296, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 5: invalid continuation byte
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /" - 500 Internal Server Error
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /favicon.ico" - 404 Not Found
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /favicon.ico" - 404 Not Found
,我不認爲有我的代碼有些問題,因爲我的代碼在我的計算機上運行得非常好,只有當它在服務器上運行時纔會出現錯誤。 「web」目錄是「web.py-0.34/web」的鏈接,它不是我的代碼。
我的代碼很簡單:
urls = (
'/', 'find_alternate',
'/find_alternates', 'find_alternate',
'/show_detail/(.+)', 'show_detail'
)
app = web.application(urls, globals())
class find_alternate:
def GET(self):
brands = [b.brandName for b in Brand.q.all()]
brands.sort()
manus = [oe.brandName for oe in OeNumber.q.group_by(OeNumber.brandName)]
manus.sort()
return render.find_alternates_main(simplejson.dumps(brands), simplejson.dumps(manus))
"""
some more functions, but not relevant
"""
render = web.template.render('/path/to/templates/')
web.template.Template.globals['str'] = str
if __name__ == "__main__":
app.run()
我的CREATE TABLE:現在
CREATE TABLE `brand` (
`brandNo` int(11) NOT NULL,
`brandName` varchar(64) DEFAULT NULL,
PRIMARY KEY (`brandNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
我的問題是藿字符Ë從Unicode轉換爲UTF-8,這樣jsonsimple可以解析它。在維基我發現這一點:
Unicode: U+00CB
UTF-8: C3(hex) 8B(hex)
如何固溶: 添加以下行my.cnf文件:
collation-server = utf8_unicode_ci
init_connect='SET NAMES utf8'
character-set-server = utf8
skip-character-set-client-handshake
數據庫轉換爲UTF-8:
ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
「我不認爲有什麼問題」是不夠的。請寄出重要的一段代碼。 – jazzpi
顯然,你的代碼或者你的web服務器配置,或者你訪問它的方式都有問題。既然你沒有給我們任何關於這三者的信息,那麼任何人都無法幫助你。除了給你一些提示,你可能出於某種原因發送非UTF-8數據(例如Latin-1或Windows CP1252),並且你的代碼或web.py試圖將它解釋爲UTF-8原因。如果你還沒有閱讀[Unicode HOWTO](http://docs.python.org/2/howto/unicode.html),現在可能是個好時機。 – abarnert
sry,我沒有說完我的話和missclicked「post」 – Cricket