使用 OpenStack 传递会话对象 - KeyStone
Passing Session Object Using OpenStack - KeyStone
我正在尝试将会话对象从一个文件传递到另一个文件,以便在我需要做的所有不同事情(list/create 虚拟机等)中使用相同的会话对象。到目前为止,我我已经尝试过使用 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
问题出在 loader
对象上。
您应该在接收方加载授权凭据
您可以 pickle loader
和选项,但不能 pickle auth
对象
我正在尝试将会话对象从一个文件传递到另一个文件,以便在我需要做的所有不同事情(list/create 虚拟机等)中使用相同的会话对象。到目前为止,我我已经尝试过使用 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
问题出在 loader
对象上。
您应该在接收方加载授权凭据
您可以 pickle loader
和选项,但不能 pickle auth
对象