我正在调用 maas api 来获取节点的详细信息。我得到的输出是 bson 数据。我该如何迭代它。通过搜索,我找到了那个 BSON 包。但我想使用它的替代方案。你能推荐其他包来完成这项工作吗?
我的 maas api 调用如下
resp = self.client.get(u"nodes/%s/" % node_id, "details")
打印响应给出 =>
<addinfourl at 139851033511408 whose fp = <gzip _io.BytesIO object at 0x7f319b4f7dd0 0x7f319b2abd50>>
print resp.header
Date: Tue, 28 Jun 2016 10:16:26 GMT
Server: TwistedWeb/13.2.0
Content-Length: 3007
Content-Encoding: gzip
Vary: Authorization,Accept-Encoding,Cookie
X-Maas-Api-Hash: 0e5a2694573dce65d8051aa612b2ee9093b62c92
X-Frame-Options: SAMEORIGIN
Content-Type: application/bson
Connection: close
print resp.read() Gives following o/p
> �8lldpF<?xml version="1.0" encoding="UTF-8"?>
<lldp label="LLDP neighbors"/>
lshwm8<?xml version="1.0" standalone="yes" ?>
<!-- generated by lshw-B.02.16 -->
<!-- GCC 4.8.4 -->
<!-- Linux 3.13.0-85-generic #129-Ubuntu SMP Thu Mar 17 20:50:15 UTC 2016 x86_64 -->
<!-- GNU libc 2 (glibc 2.19) -->
<list>
<node id="crisp-change" claimed="true" class="system" handle="DMI:0100">
<description>Computer</description>
<product>Standard PC (i440FX + PIIX, 1996) ()</product>
<vendor>QEMU</vendor>
<version>pc-i440fx-trusty</version>
<width units="bits">64</width>
<configuration>
<setting id="boot" value="normal" />
<setting id="uuid" value="B0E5BE78-766F-E75F-6509-14AD45C92EAB" />
</configuration>
<capabilities>
<capability id="smbios-2.4" >SMBIOS version 2.4</capability>
<capability id="dmi-2.4" >DMI version 2.4</capability>
<capability id="vsyscall32" >32-bit processes</capability>
</capabilities>
<node id="core" claimed="true" class="bus" handle="">
<description>Motherboard</description>
<physid>0</physid>
<node id="firmware" claimed="true" class="memory" handle="">
<description>BIOS</description>
<vendor>Bochs</vendor>
<physid>0</physid>
<version>Bochs</version>
<date>01/01/2011</date>
<size units="bytes">98304</size>
</node>
<node id="cpu" claimed="true" class="processor" handle="DMI:0401">
<description>CPU</description>
<product>QEMU Virtual CPU version 2.0.0</product>
<vendor>Advanced Micro Devices [AMD]</vendor>
<physid>401</physid>
<businfo>cpu@0</businfo>
<slot>CPU 1</slot>
<size units="Hz">2000000000</size>
<capacity units="Hz">2000000000</capacity>
<width units="bits">64</width>
<capabilities>
<capability id="fpu" >mathematical co-processor</capability>
<capability id="fpu_exception" >FPU exceptions reporting</capability>
<capability id="wp" />
<capability id="de" >debugging extensions</capability>
<capability id="pse" >page size extensions</capability>
<capability id="tsc" >time stamp counter</capability>
<capability id="msr" >model-specific registers</capability>
<capability id="pae" >4GB+ memory addressing (Physical Address Extension)</capability>
<capability id="mce" >machine check exceptions</capability>
<capability id="cx8" >compare and exchange 8-byte</capability>
<capability id="apic" >on-chip advanced programmable interrupt controller (APIC)</capability>
<capability id="sep" >fast system calls</capability>
<capability id="mtrr" >memory type range registers</capability>
<capability id="pge" >page global enable</capability>
<capability id="mca" >machine check architecture</capability>
<capability id="cmov" >conditional move instruction</capability>
<capability id="pat" >page attribute table</capability>
<capability id="pse36" >36-bit page size extensions</capability>
<capability id="clflush" />
<capability id="mmx" >multimedia extensions (MMX)</capability>
<capability id="fxsr" >fast floating point save/restore</capability>
<capability id="sse" >streaming SIMD extensions (SSE)</capability>
<capability id="sse2" >streaming SIMD extensions (SSE2)</capability>
<capability id="syscall" >fast system calls</capability>
<capability id="nx" >no-execute bit (NX)</capability>
<capability id="x86-64" >64bits extensions (x86-64)</capability>
<capability id="nopl" />
<capability id="pni" />
<capability id="cx16" />
<capability id="x2apic" />
<capability id="popcnt" />
<capability id="hypervisor" />
<capability id="lahf_lm" />
<capability id="svm" />
<capability id="abm" />
<capability id="sse4a" />
<capability id="vmmcall" />
</capabilities>
</node>
<node id="memory" claimed="true" class="memory" handle="DMI:1000">
<description>System Memory</description>
<physid>1000</physid>
<size units="bytes">1073741824</size>
<capacity units="bytes">1073741824</capacity>
<node id="bank" claimed="true" class="memory" handle="DMI:1100">
<description>DIMM RAM</description>
<physid>0</physid>
<slot>DIMM 0</slot>
<size units="bytes">1073741824</size>
<width units="bits">64</width>
</node>
</node>
<node id="pci" claimed="true" class="bridge" handle="PCIBUS:0000:00">
<description>Host bridge</description>
<product>440FX - 82441FX PMC [Natoma]</product>
<vendor>Intel Corporation</vendor>
<physid>100</physid>
<businfo>pci@0000:00:00.0</businfo>
<version>02</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<node id="isa" claimed="true" class="bridge" handle="PCI:0000:00:01.0">
<description>ISA bridge</description>
<product>82371SB PIIX3 ISA [Natoma/Triton II]</product>
<vendor>Intel Corporation</vendor>
<physid>1</physid>
<businfo>pci@0000:00:01.0</businfo>
<version>00</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="isa" />
</capabilities>
</node>
<node id="ide" claimed="true" class="storage" handle="PCI:0000:00:01.1">
<description>IDE interface</description>
<product>82371SB PIIX3 IDE [Natoma/Triton II]</product>
<vendor>Intel Corporation</vendor>
<physid>1.1</physid>
<businfo>pci@0000:00:01.1</businfo>
<version>00</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="driver" value="ata_piix" />
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="ide" />
<capability id="bus_master" >bus mastering</capability>
</capabilities>
<resources>
<resource type="irq" value="0" />
<resource type="ioport" value="1f0(size=8)" />
<resource type="ioport" value="3f6" />
<resource type="ioport" value="170(size=8)" />
<resource type="ioport" value="376" />
<resource type="ioport" value="c0a0(size=16)" />
</resources>
</node>
<node id="usb" claimed="true" class="bus" handle="PCI:0000:00:01.2">
<description>USB controller</description>
<product>82371SB PIIX3 USB [Natoma/Triton II]</product>
<vendor>Intel Corporation</vendor>
<physid>1.2</physid>
<businfo>pci@0000:00:01.2</businfo>
<version>01</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="driver" value="uhci_hcd" />
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="uhci" >Universal Host Controller Interface (USB1)</capability>
<capability id="bus_master" >bus mastering</capability>
</capabilities>
<resources>
<resource type="irq" value="11" />
<resource type="ioport" value="c040(size=32)" />
</resources>
</node>
<node id="bridge" claimed="true" class="bridge" handle="PCI:0000:00:01.3">
<description>Bridge</description>
<product>82371AB/EB/MB PIIX4 ACPI</product>
<vendor>Intel Corporation</vendor>
<physid>1.3</physid>
<businfo>pci@0000:00:01.3</businfo>
<version>03</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="driver" value="piix4_smbus" />
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="bridge" />
</capabilities>
<resources>
<resource type="irq" value="9" />
</resources>
</node>
<node id="display" class="display" handle="PCI:0000:00:02.0">
<description>VGA compatible controller</description>
<product>GD 5446</product>
<vendor>Cirrus Logic</vendor>
<physid>2</physid>
<businfo>pci@0000:00:02.0</businfo>
<version>00</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="vga_controller" />
</capabilities>
<resources>
<resource type="memory" value="fc000000-fdffffff" />
<resource type="memory" value="febd0000-febd0fff" />
<resource type="memory" value="febc0000-febcffff" />
</resources>
</node>
<node id="network" claimed="true" class="network" handle="PCI:0000:00:03.0">
<description>Ethernet interface</description>
<product>Virtio network device</product>
<vendor>Red Hat, Inc</vendor>
<physid>3</physid>
<businfo>pci@0000:00:03.0</businfo>
<logicalname>eth0</logicalname>
<version>00</version>
<serial>52:54:00:c4:65:b4</serial>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="broadcast" value="yes" />
<setting id="driver" value="virtio_net" />
<setting id="driverversion" value="1.0.0" />
<setting id="ip" value="10.20.0.151" />
<setting id="latency" value="0" />
<setting id="link" value="yes" />
<setting id="multicast" value="yes" />
</configuration>
<capabilities>
<capability id="msix" >MSI-X</capability>
<capability id="bus_master" >bus mastering</capability>
<capability id="cap_list" >PCI capabilities listing</capability>
<capability id="rom" >extension ROM</capability>
<capability id="ethernet" />
<capability id="physical" >Physical interface</capability>
</capabilities>
<resources>
<resource type="irq" value="10" />
<resource type="ioport" value="c060(size=32)" />
<resource type="memory" value="febd1000-febd1fff" />
<resource type="memory" value="feb80000-febbffff" />
</resources>
</node>
<node id="scsi" claimed="true" class="storage" handle="PCI:0000:00:04.0">
<description>SCSI storage controller</description>
<product>Virtio block device</product>
<vendor>Red Hat, Inc</vendor>
<physid>4</physid>
<businfo>pci@0000:00:04.0</businfo>
<version>00</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="driver" value="virtio-pci" />
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="scsi" />
<capability id="msix" >MSI-X</capability>
<capability id="bus_master" >bus mastering</capability>
<capability id="cap_list" >PCI capabilities listing</capability>
</capabilities>
<resources>
<resource type="irq" value="11" />
<resource type="ioport" value="c000(size=64)" />
<resource type="memory" value="febd2000-febd2fff" />
</resources>
</node>
<node id="generic" claimed="true" class="generic" handle="PCI:0000:00:05.0">
<description>Unclassified device</description>
<product>Virtio memory balloon</product>
<vendor>Red Hat, Inc</vendor>
<physid>5</physid>
<businfo>pci@0000:00:05.0</businfo>
<version>00</version>
<width units="bits">32</width>
<clock units="Hz">33000000</clock>
<configuration>
<setting id="driver" value="virtio-pci" />
<setting id="latency" value="0" />
</configuration>
<capabilities>
<capability id="bus_master" >bus mastering</capability>
</capabilities>
<resources>
<resource type="irq" value="10" />
<resource type="ioport" value="c080(size=32)" />
</resources>
</node>
</node>
<node id="scsi" claimed="true" class="storage" handle="">
<physid>1</physid>
<logicalname>scsi2</logicalname>
<node id="generic" class="generic" handle="SCSI:02:00:00:00">
<description>SCSI</description>
<product>Controller</product>
<vendor>IET</vendor>
<physid>0.0.0</physid>
<businfo>scsi@2:0.0.0</businfo>
<version>0001</version>
<serial>beaf10</serial>
<configuration>
<setting id="ansiversion" value="5" />
</configuration>
</node>
<node id="disk" claimed="true" class="volume" handle="SCSI:02:00:00:01">
<description>EXT4 volume</description>
<product>VIRTUAL-DISK</product>
<vendor>Linux</vendor>
<physid>0.0.1</physid>
<businfo>scsi@2:0.0.1</businfo>
<logicalname>/dev/sda</logicalname>
<logicalname>/media/root-ro</logicalname>
<dev>8:0</dev>
<version>1.0</version>
<serial>6872cb1e-3766-4ff3-9de5-11ffd99c3fc9</serial>
<size units="bytes">1468006400</size>
<configuration>
<setting id="ansiversion" value="5" />
<setting id="created" value="2016-04-18 16:39:57" />
<setting id="filesystem" value="ext4" />
<setting id="label" value="cloudimg-rootfs" />
<setting id="lastmountpoint" value="/tmp/copy-source-to-target.qeycvT/tgt" />
<setting id="modified" value="2016-04-18 16:40:52" />
<setting id="mount.fstype" value="ext4" />
<setting id="mount.options" value="ro,relatime,data=ordered" />
<setting id="mounted" value="2016-04-18 16:39:58" />
<setting id="sectorsize" value="4096" />
<setting id="state" value="mounted" />
</configuration>
<capabilities>
<capability id="journaled" />
<capability id="extended_attributes" >Extended Attributes</capability>
<capability id="large_files" >4GB+ files</capability>
<capability id="huge_files" >16TB+ files</capability>
<capability id="dir_nlink" >directories with 65000+ subdirs</capability>
<capability id="extents" >extent-based allocation</capability>
<capability id="ext4" />
<capability id="ext2" >EXT2/EXT3</capability>
<capability id="initialized" >initialized volume</capability>
</capabilities>
</node>
</node>
</node>
</node>
</list>
我如何才能从中迭代这些值。
答案1
我假设你正在使用 USE 团队的 MAAS 客户端,来自https://github.com/Ubuntu-Solutions-Engineering/maasclient。该客户端以 BSON 格式返回查询数据。
要解析 BSON 输出,请使用 Python BSON 包:https://github.com/py-bson/bson
以下是您读取案例中的数据的方法:
import bson
resp = self.client.get(u"nodes/%s/" % node_id, "details")
data = bytes(rest.read(), "utf8")
node_details = bson.loads(data)
注意:我想用一个例子来更新这个答案,这需要等到我的 MAAS 2.0 安装更新。
如果您不想使用 BSON,那么您应该考虑使用目前与 MAAS 本身捆绑在一起的简单 MAAS 客户端。我还将更新答案并提供有关此内容的详细信息。