Flask 0.10在会话cookie上给出unicode错误

时间:2013-06-13 21:18:47

标签: python flask flask-login

升级到Flask 0.10后,我收到此错误:

flask.debughelpers.UnexpectedUnicodeError
UnexpectedUnicodeError: A byte string with non-ASCII data was passed to the session system which can only store unicode strings.  Consider base64 encoding your string (String was 'iB\rOU#\xf7BO\x08^\xa6\xd1)v\xad')

Traceback (most recent call last)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 1479, in full_dispatch_request
response = self.process_response(response)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 1693, in process_response
self.save_session(ctx.session, response)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/app.py", line 837, in save_session
return self.session_interface.save_session(self, session, response)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/sessions.py", line 321, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "/srv/www/li/venv/lib/python2.7/site-packages/itsdangerous.py", line 471, in dumps
return self.make_signer(salt).sign(self.dump_payload(obj))
File "/srv/www/li/venv/lib/python2.7/site-packages/itsdangerous.py", line 676, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "/srv/www/li/venv/lib/python2.7/site-packages/itsdangerous.py", line 454, in dump_payload
return self.serializer.dumps(obj)
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/sessions.py", line 82, in dumps
return json.dumps(_tag(value), separators=(',', ':'))
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/sessions.py", line 72, in _tag
return dict((k, _tag(v)) for k, v in iteritems(value))
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/sessions.py", line 72, in <genexpr>
return dict((k, _tag(v)) for k, v in iteritems(value))
File "/srv/www/li/venv/lib/python2.7/site-packages/flask/sessions.py", line 80, in _tag
u'base64 encoding your string (String was %r)' % value)
UnexpectedUnicodeError: A byte string with non-ASCII data was passed to the session system which can only store unicode strings. Consider base64 encoding your string (String was 'iB\rOU#\xf7BO\x08^\xa6\xd1)v\xad')

flask / app.py第1693行给出的self.save_session(ctx.session,response)是这样的: flask.sessions.SecureCookieSession({'_ id':'iB \ rOU#\ xf7BO \ x08 ^ \ xa6 \ xd1)v \ xad','_ flashes':[('message','请登录访问此页面。 “)]})

所以,如果我正确地读到这个,那么会话会给出一个烧瓶尝试unicode(并且失败)的id。我不知道如何纠正这个问题(清空我的浏览器缓存以希望新的会话也无济于事)。 谁能给我任何关于我应该做什么的建议?

亲切的问候,

岩溶

编辑:一些额外的信息。我的设置使用flask,flask_mail,flask.ext.mongoengine和flask.ext.security;我使用MongoEngine:     db = flask.ext.mongoengine.MongoEngine(app) 和烧瓶安全:     user_datastore = flask.ext.security.MongoEngineUserDatastore(db,User,Role)     security = flask.ext.security.Security(app,user_datastore)

编辑:这似乎是一个双重问题: Flask Login: TypeError: decoding Unicode is not supported

(没见过,抱歉)

2 个答案:

答案 0 :(得分:10)

Flask-Login目前存在问题。据我所知,它已在扩展程序的git版本中修复。

答案 1 :(得分:2)

临时解决方法(感谢Kave)正在降级Flask和Werkzeug:

pip install werkzeug==0.8.3
pip install flask==0.9
pip install Flask-Login==0.1.3