我有一个查询成本管理API的Python程序。这是工作正常,但后来我开始得到越来越多的{“code”:“429”,“message”:“Too many requests. Please retry."}错误。请建议我应该怎么做?这可能是资源管理器上为订阅设置的限制的结果。是否应该在标题中有一个“Retry-after”?请帮助。我试着等待或“睡眠”一分钟。
6rqinv9w1#
错误是暂时的,当我们发送多个请求到成本管理API时发生,当我运行下面的代码时,我得到了所需的输出:-
代码:-
import requests import time from azure.identity import DefaultAzureCredential from azure.mgmt.costmanagement import CostManagementClient def main(): client = CostManagementClient( credential=DefaultAzureCredential(), ) response = client.query.usage( scope="subscriptions/<subscription-id>", parameters={ "dataset": { "filter": { "and": [ { "or": [ { "dimensions": { "name": "ResourceLocation", "operator": "In", "values": ["East US", "West Europe", "UK South"], } }, {"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}}, ] }, {"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}}, ] }, "granularity": "Daily", }, "timeframe": "MonthToDate", "type": "Usage", }, ) print(response) # x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountQuery.json if __name__ == "__main__": main()
当我再次运行代码时,我收到了相同的错误,因为后端服务器无法同时检索多个请求,参考下面:
我等了5-10分钟,再次运行代码,再次收到所需的输出:-
作为替代方案,您可以直接从API管理工具或Postman调用API,如下所示:
scope= subscriptions/subscription-id正文=
{ "type": "Usage", "timeframe": "MonthToDate", "dataset": { "granularity": "Daily", "filter": { "and": [ { "or": [ { "dimensions": { "name": "ResourceLocation", "operator": "In", "values": [ "East US", "West Europe" ] } }, { "tags": { "name": "Environment", "operator": "In", "values": [ "UAT", "Prod" ] } } ] }, { "dimensions": { "name": "ResourceGroup", "operator": "In", "values": [ "API" ] } } ] } } }
输出:-
您可以在这里的输出中检查您的成本管理API速率限制配额,并在代码中相应地计划您的标头,参考下面:
x-ms-ratelimit-microsoft.costmanagement-qpu-consumed: 1 x-ms-ratelimit-microsoft.costmanagement-qpu-remaining: QueriesPerHour:597,QueriesPerMin:58,QueriesPer10Sec:11 x-ms-ratelimit-remaining-microsoft.costmanagement-clienttype-requests: DefaultQuota:2616 x-ms-ratelimit-remaining-microsoft.costmanagement-entity-requests: DefaultQuota:2 x-ms-ratelimit-remaining-microsoft.costmanagement-tenant-requests: DefaultQuota:18 x-ms-ratelimit-remaining-subscription-resource-requests: 98
输出响应:-
参考资料:-
查询-使用情况- REST API(Azure Cost Management)|微软学习python - requests.exceptions.HTTPError: 429 Client Error: Too Many Requests for url - Stack Overflow By Daweo
1条答案
按热度按时间6rqinv9w1#
错误是暂时的,当我们发送多个请求到成本管理API时发生,当我运行下面的代码时,我得到了所需的输出:-
代码:-
当我再次运行代码时,我收到了相同的错误,因为后端服务器无法同时检索多个请求,参考下面:
我等了5-10分钟,再次运行代码,再次收到所需的输出:-
作为替代方案,您可以直接从API管理工具或Postman调用API,如下所示:
scope= subscriptions/subscription-id
正文=
输出:-
您可以在这里的输出中检查您的成本管理API速率限制配额,并在代码中相应地计划您的标头,参考下面:
输出响应:-
参考资料:-
查询-使用情况- REST API(Azure Cost Management)|微软学习
python - requests.exceptions.HTTPError: 429 Client Error: Too Many Requests for url - Stack Overflow By Daweo