以json格式的Openstack输出

时间:2016-08-24 03:39:40

标签: json python-2.7 novaclient

我正在使用novaclient从Openstack获取详细信息。我能够检索信息,但我想将其转换为json格式。我正在使用“to_dic()”但它;在“server_details = server_id_name.to_dict()”上抛出“属性”错误,不知道为什么。

AttributeError: "'tuple' object has no attribute 'to_dict'"

代码如下,

from novaclient import client as novaclient
import json

nova = novaclient.Client(version='2.0',username='xxxx',api_key='xxxx',project_id='xxxx',auth_url='http://192.168.12.3:5000/v2.0/',insecure='True')

server_details = dict()
server = nova.servers.list()
for server in nova.servers.list():
    print server.id, server.name
    server_id_name = server.id, server.name
    server_details = server_id_name.to_dict()
    for network in server.networks.items():
        print network

1 个答案:

答案 0 :(得分:1)

你的python代码中有错误。 你应该使用

server_details[server.id] = server.name

替换你的代码

server_id_name = server.id, server.name
server_details = server_id_name.to_dict()
顺便说一句,你应该学习更多关于python的知识。如果要将元组更改为dict,则应该看到python-tuple-to-dict