Python API 调用迭代

Python API Call Iteration

我正在使用 Meraki api 创建 l7 防火墙批量更新 python 脚本,我 运行 遇到了问题。我需要脚本遍历 .txt 文件中的每个行项目。 .txt 文件中的每个行项目都是一个网络 ID。目前,我的代码只使用列表顶部的网络 ID 设置 运行s 一次。我该如何补救?

import requests
import json
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.environ.get("API_Key")

with open('testnetids.txt') as file:
    for line in file:
        line = line.rstrip("\n")

url = "https://api.meraki.com/api/v0/networks/%s/l7FirewallRules" % line

with open("layer7rules.json") as f:
    payload = json.load(f)

headers = {
    'X-Cisco-Meraki-API-Key': api_key,
    'Content-Type': 'application/json',
}

response = requests.put(url, data=json.dumps(payload), headers=headers)

print(response.text)

为了对文件中的每个行进行一些处理,您需要将实际处理移到 [=11] =]循环:

import requests
import json
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.environ.get("API_Key")

with open('testnetids.txt') as file:
    for line in file:
        line = line.rstrip("\n")

        url = "https://api.meraki.com/api/v0/networks/%s/l7FirewallRules" % line

        with open("layer7rules.json") as f:
            payload = json.load(f)

            headers = {
                'X-Cisco-Meraki-API-Key': api_key,
                'Content-Type': 'application/json',
            }

            response = requests.put(url, data=json.dumps(payload), headers=headers)

            print(response.text)