使用OpenStack传递会话对象 - KeyStone

时间:2017-10-17 10:41:00

标签: python json openstack cherrypy keystone

我试图将会话对象从一个文件传递到另一个文件,以便在我需要做的所有不同事情(列表/创建VM等)中使用相同的会话对象。到目前为止,我是尝试使用Pickle(由于对象上有锁定实例,因此没有工作)。我现在正在尝试使用Cherrypy来将对象存储为cookie(我已经为name和fedid做了)。但我收到的错误是会话对象不是JSON可序列化的。

如何解决此错误,这是传递会话对象的正确方法吗?如果它不是正确的解决方案,我如何传递会话对象(文档不是很有用)?

import cherrypy
import ldap
import xmlrpclib
import xml.etree.ElementTree as ET
from keystoneauth1 import loading
from keystoneauth1 import session as session
import novaclient.client as client
import json
import pickle


NOVA_VERSION="2.1"
KEYSTONE_URL = cherrypy.request.config.get("keystone")
OPENSTACK_HOST = cherrypy.request.config.get("openstack_host")
OPENSTACK_DEFAULT_DOMAIN = 
cherrypy.request.config.get("openstack_default_domain")
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=KEYSTONE_URL, username=data.get("username"), password=data.get("password").replace(" ","%20"),user_domain_name=OPENSTACK_DEFAULT_DOMAIN)
sess = session.Session(auth=auth)
nova = client.Client(NOVA_VERSION,session=sess)
data = {
        'name'    : name,
        'fedid'   : data.get("username"),
        'sessionTest' : json.dumps(sess),
        'expires' : EXPIRE
    }

错误:

File "/usr/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/cherrypy/lib/jsontools.py", line 61, in json_handler
value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "/var/www/frontend/controllers/api/user.py", line 102, in PUT
'sessionTest' : json.dumps(sess),
File "/usr/lib64/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <keystoneauth1.session.Session object at 0x7f023003c710> is not JSON serializable

1 个答案:

答案 0 :(得分:0)

问题在于loader对象。

您应该在接收方

加载身份验证凭据

你可以挑选loader和选项而不是auth对象