Python 软层API超时

Python 软层API超时,python,ibm-cloud-infrastructure,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, f

我正在使用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'))

在进程运行时,没有关闭/断开客户端连接的方法

我能够用150多个硬件项目重现错误,而不是更少。有时,由于掩码很长,获取所有数据需要花费更多的时间,我建议尽可能减少掩码

互联网连接可能会影响您获得的结果,我建议您重新查看

如果你想得到所有的BMS,你可以试着一部分一部分地得到它们,下面你可以看到一个例子。 如果错误仍然存在,您可以通过逐个检索它们来尝试(limit=1)

“”“
列出裸机服务器。
该脚本逐部分检索所有裸机服务器,以避免超时
错误。
重要手册页:
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
许可证:http://sldn.softlayer.com/article/License
作者:SoftLayer Technologies,Inc。
"""
导入软层
#您的SoftLayer API用户名和密钥。
用户名='设置我'
API_KEY='设置我'
#resultLimit功能的限制和偏移量。
限值=10
偏移量=0
#存储所有SoftLayer_硬件对象的阵列。
硬件=[]
#标记以了解是否仍有SoftLayer_硬件
has_items=True
#创建客户端实例
客户端=软件层。从环境创建客户端(用户名=用户名,api密钥=api密钥)
object_mask='id,FullyQualifiedDomain,operatingSystem.softwareLicense'\
“.softwareDescription.longDescription,hardwareChassis.manufacturer,”\
'hardwareChassis.name,hardwareChassis.version,networkComponents'\
'.PrimaryPaddress,processorCount,数据中心.name,'\
'主后端IP地址,主板。硬件组件模型'\
'.longDescription,processors.hardwareComponentModel.longDescription,'\
'memory.hardwareComponentModel.longDescription,'\
'内存.hardwareComponentModel.capacity,RAID控制器'\
“.hardwareComponentModel.longDescription,硬盘驱动器”\
“.hardwareComponentModel.longDescription”
尝试:
"""
下面的循环有助于逐部分检索所有数据
检索每10个对象。如果需要,只需更改限制值
增加或减少检索数据的数量。
"""
while有以下项目:
硬件\u项=客户端['SoftLayer\u帐户]。getHardware(掩码=对象\u掩码,
极限=极限,
偏移量=偏移量)
硬件.扩展(硬件\u项)
如果len(硬件项目)>0:
偏移量+=极限
其他:
has\u items=False
打印(硬件)
除SoftLayer.SoftLayer外,错误为e:
"""
如果SoftLayer API返回错误,则使用
错误消息。
"""
打印(“无法获取SoftLayer\u硬件对象:%s,%s”%(e.faultCode,
e、 错误字符串)
我希望这对你有帮助

问候

"""
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))