flask-cache TypeError:无法腌制CompiledFFI对象

时间:2019-10-06 09:17:03

标签: python flask flask-cache

我正在python中使用pysaml2库。其中有一个method defined as below

def _store_request(self, saml_msg):
    key = sha1(saml_msg["SAMLRequest"]).hexdigest()
    IDP.ticket[key] = saml_msg
    return key

其中IDP.ticket = {}是应用程序范围内的简单键值映射(在raw WSGI application中)。此处IDP.ticket在多个请求之间保持不变。下面的一些上下文。

被调用的路由为SSO().redirect

  • 最初self.userFalse,因此密钥由 self._store_request(saml_msg)
  • 然后向用户返回一个表单,并在表单中设置了密钥,以便可以识别请求。在表单上,​​提交SSO().redirect再次被调用,并且在先前请求期间在IDP.ticket中设置了值。

我想将此方法移植到flask应用程序,但是当我cache.set(key,saml_msg)使用烧瓶与Flask-Cache时出现错误:

TypeError: can't pickle CompiledFFI objects

我猜测是因为saml_msg包含密钥

req_info': <saml2.request.AuthnRequest object at 0x1072302d0>

除了使用FLask-Cache之外,如何在烧瓶中的多个请求之间存储状态?

编辑:

这个问题类似于this question,但我已经尝试过该解决方案。我需要保存一个object而不是简单的数据类型,因此当我使用Flask-Cache保存它时,我会得到“可以” t腌制CompiledFFI对象(也许是AuthnRequest对象的东西,不能腌制)

使用flask.session在请求之间存储密钥,它得到TypeError: Object of type AuthnRequest is not JSON serializable

完整堆栈跟踪。

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1969, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2268, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.7/site-packages/flask/sessions.py", line 378, in save_session
    val = self.get_signing_serializer(app).dumps(dict(session))
  File "/usr/local/lib/python3.7/site-packages/itsdangerous/serializer.py", line 166, in dumps
    payload = want_bytes(self.dump_payload(obj))
  File "/usr/local/lib/python3.7/site-packages/itsdangerous/url_safe.py", line 42, in dump_payload
    json = super(URLSafeSerializerMixin, self).dump_payload(obj)
  File "/usr/local/lib/python3.7/site-packages/itsdangerous/serializer.py", line 133, in dump_payload
    return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
  File "/usr/local/lib/python3.7/site-packages/flask/json/tag.py", line 305, in dumps
    return dumps(self.tag(value), separators=(",", ":"))
  File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 211, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 100, in default
    return _json.JSONEncoder.default(self, o)
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type AuthnRequest is not JSON serializable

0 个答案:

没有答案