Softlayer API超时

时间:2017-03-22 02:32:02

标签: python ibm-cloud-infrastructure

我正在使用Softlayer Python API来提取硬件细节。该脚本中途退出,出现API错误。我已经将服务器数量限制为10,但仍然是错误。

我们有办法关闭/断开与api的连接吗? 例如,如何在下面明确关闭“客户端”? 有什么帮助来克服这个问题吗?

client = SoftLayer.Client(username=USER, api_key=API_KEY,timeout = 1000)

hardware = client['Account'].getHardware(mask='id, fullyQualifiedDomainName,operatingSystem.softwareLicense.softwareDescription.longDescription,hardwareChassis.manufacturer,hardwareChassis.name,hardwareChassis.version,networkComponents.primaryIpAddress,processorCount,datacenter.name,primaryBackendIpAddress,motherboard.hardwareComponentModel.longDescription,processors.hardwareComponentModel.longDescription,memory.hardwareComponentModel.longDescription,memory.hardwareComponentModel.capacity,raidControllers.hardwareComponentModel.longDescription,hardDrives.hardwareComponentModel.longDescription',limit=limit,offset=offset);


File "/usr/local/bin/inv.py", line 90, in fetch_hw
hardware = client['Account'].getHardware(mask='id, fullyQualifiedDomainName,operatingSystem.softwareLicense.softwareDescription.longDescription,hardwareChassis.manufacturer,hardwareChassis.name,hardwareChassis.version,networkComponents.primaryIpAddress,processorCount,datacenter.name,primaryBackendIpAddress,motherboard,processors,memory,raidControllers,hardDrives',limit=limit,offset=offset);
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 392, in call_handler
return self(name, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 360, in call
return self.client.call(self.name, name, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 263, in call
return self.transport(request)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/transports.py", line 199, in __call__
raise exceptions.TransportError(0, str(ex))
SoftLayer.exceptions.TransportError: TransportError(0): ("Connection broken: error(104, 'Connection reset by peer')", error(104, 'Connection reset by peer'))

1 个答案:

答案 0 :(得分:0)

进程运行时无法关闭/断开客户端。

我能够用超过150个硬件项重现错误,而不是更少。有时需要更多的时间来获取所有数据,因为你的面具很长,我建议你尽可能地减少它。

互联网连接可能会影响您获得的结果,我建议您进行审核。

如果您想获得所有BMS,您可以尝试逐个获取所有BMS,下面您可以看到一个示例。 如果错误仍然存​​在,您可以尝试逐个检索(limit = 1)

"""
List Bare Metal servers.

The script retrieve all bare metal servers part by part in order to avoid time out
errors.

Important manual pages:
https://sldn.softlayer.com/reference/services/SoftLayer_Account
https://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware
https://sldn.softlayer.com/reference/datatype/SoftLayer_Hardware/
https://sldn.softlayer.com/article/object-masks
https://sldn.softlayer.com/article/object-filters

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer

# Your SoftLayer API username and key.
USERNAME = 'set-me'
API_KEY = 'set-me'

# The limit and offset of resultLimit feature.
limit = 10
offset = 0

# The array where all SoftLayer_Hardware objects will be stored.
hardware = []

# Flag to know if there are still SoftLayer_Hardware
has_items = True

# Create client instance
client = SoftLayer.create_client_from_env(username=USERNAME, api_key=API_KEY)

object_mask = 'id,fullyQualifiedDomainName,operatingSystem.softwareLicense' \
              '.softwareDescription.longDescription,hardwareChassis.manufacturer,' \
              'hardwareChassis.name,hardwareChassis.version,networkComponents' \
              '.primaryIpAddress,processorCount,datacenter.name,' \
              'primaryBackendIpAddress,motherboard.hardwareComponentModel' \
              '.longDescription,processors.hardwareComponentModel.longDescription,' \
              'memory.hardwareComponentModel.longDescription,' \
              'memory.hardwareComponentModel.capacity,raidControllers' \
              '.hardwareComponentModel.longDescription,hardDrives' \
              '.hardwareComponentModel.longDescription'

try:
    """
    Following loop helps to retrieve all data part by part. This example shows how
    to retrieve each 10 objects. Just change limit value if you want to
    increase or decrease the number of retrieved data.
    """
    while has_items:
        hardware_items = client['SoftLayer_Account'].getHardware(mask=object_mask,
                                                                 limit=limit,
                                                                 offset=offset)
        hardware.extend(hardware_items)

        if len(hardware_items) > 0:
            offset += limit
        else:
            has_items = False

    print(hardware)

except SoftLayer.SoftLayerAPIError as e:
    """
    If there was an error returned from the SoftLayer API then bomb out with
    the error message.
    """
    print("Unable to get SoftLayer_Hardware objects: %s, %s " % (e.faultCode,
                                                                 e.faultString))

我希望这对你有所帮助。

问候。