【问题标题】:In python, how to try/except the list of API keys [closed]在 python 中,如何尝试/排除 API 密钥列表 [关闭]
【发布时间】:2022-11-16 13:48:57
【问题描述】:

我在列表中有几个 API 密钥:


keys = [
'key1',
'key2',
'key3',
...]

由于每日使用限制,我想在每个应用程序上使用 try/except 进行 API 调用。有没有聪明的方法来做到这一点?

【问题讨论】:

  • 使用 for key in keys: 遍历每个键,并在循环内,使用当前键在 api 调用周围放置一个 try/except 块。有什么困难?

标签: python api try-catch


【解决方案1】:

怎么样:

keys = ['key1', 'key2', 'key3']

for key in keys:
    try:
        # API call here
    except:
        print(f"API key {key} didn't work because (REASON)")
        continue

continue 继续回到循环的开始以尝试下一个 API 密钥。它实际上在这里做了任何事情,因为 except 块无论如何都在循环的末尾,但为了清楚起见,最好包含它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2011-08-25
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多