python - 迭代 VM 列表时出现 Azure 异步 Python SDK 错误

标签 python azure api

我制作了一个简单的异步 python 程序来获取订阅中所有虚拟机的列表。但我在遍历列表时遇到错误。

完整的程序:

from azure.identity.aio import AzureCliCredential
from azure.mgmt.compute.aio import ComputeManagementClient
from azure.mgmt.network.aio import NetworkManagementClient
from azure.mgmt.web.aio import WebSiteManagementClient
from azure.mgmt.sql.aio import SqlManagementClient
import asyncio

credential = AzureCliCredential()

compute_client = ComputeManagementClient(credential, "XXXXX")
web_client = WebSiteManagementClient(credential, "XXXX")

async def get_as_list():
    as_list = web_client.web_apps.list()
    async for _as in as_list:
        pass
    return as_list
async def get_vm_list():
    vm_list = compute_client.virtual_machines.list_all()
    async for vm in vm_list:
        pass
    return vm_list

async def main():
    await get_as_list()
    await get_vm_list()


if __name__ == '__main__':
    asyncio.run(main())

错误:

Traceback (most recent call last):
  File "c:\Users\Kristian-Laptop\Desktop\Work\Azure Inventory\Inventory with UI\test.py", line 32, in <module>
    asyncio.run(main())
  File "C:\Users\Kristian-Laptop\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Kristian-Laptop\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 647, in run_until_complete
    return future.result()
  File "c:\Users\Kristian-Laptop\Desktop\Work\Azure Inventory\Inventory with UI\test.py", line 28, in main
    await get_vm_list()
  File "c:\Users\Kristian-Laptop\Desktop\Work\Azure Inventory\Inventory with UI\test.py", line 22, in get_vm_list
    async for vm in vm_list:
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\core\async_paging.py", line 163, in __anext__   
    return await self.__anext__()
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\core\async_paging.py", line 157, in __anext__   
    self._page = await self._page_iterator.__anext__()
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\core\async_paging.py", line 99, in __anext__    
    self._response = await self._get_next(self.continuation_token)
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\mgmt\compute\v2022_08_01\aio\operations\_virtual_machines_operations.py", line 1486, in get_next
    request = prepare_request(next_link)
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\mgmt\compute\v2022_08_01\aio\operations\_virtual_machines_operations.py", line 1471, in prepare_request
    _next_request_params["api-version"] = self._config.api_version
AttributeError: 'ComputeManagementClientConfiguration' object has no attribute 'api_version'

遍历 Web 应用程序列表没有问题。我只收到有关 VM 列表的错误。

最佳答案

你能试试这个吗?

credential = AzureCliCredential()

compute_config = ComputeManagementClientConfiguration(credential, "XXXXX")
compute_client = ComputeManagementClient(compute_config)
web_client = WebSiteManagementClient(credential, "XXXX")

关于python - 迭代 VM 列表时出现 Azure 异步 Python SDK 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73875964/

相关文章:

c# - 通过 API 发送 IM

swift - 解码简单的 API 数组 Swift

java - 用于启动和配置多个程序的 Python 脚本

Python argparse : Does it have to return a list?

python - "int ' 对象不可订阅”

rest - 在 REST API 中更改用户密码

python - BeautifulSoup HTMLParseError。这有什么问题吗?

powershell - 设置 ARM 模板 Web 应用程序设置

python查询azure表的所有行

c# - 无法将参数 'log' 绑定(bind)到类型 TraceWriter