Python erhalten Array -Werte aus jedem Index aus der API -AntwortPython

Python-Programme
Anonymous
 Python erhalten Array -Werte aus jedem Index aus der API -Antwort

Post by Anonymous »

Ich habe ein Skript, das einen API -Aufruf verwendet, um eine Liste der Seriennummern von Geräten abzurufen, und führt dann einen API -Anruf gegen jede dieser Seriennummern aus. Ich versuche, den Wert "Kanal" für jedes "Band" aus den folgenden JSON-API-Antwortdaten für jedes Gerät zu extrahieren.

Code: Select all

[
{
"band": 2,
"channelSettings": {
"channel": 6,
"width": 20,
"dfs": {
"radarDetected": false
}
},
"powerSettings": {
"transmitPower": 8
}
},
{
"band": 5,
"channelSettings": {
"channel": 64,
"width": 20,
"dfs": {
"radarDetected": false
}
},
"powerSettings": {
"transmitPower": 11
}
},
{
"band": 6,
"channelSettings": {
"channel": 5,
"width": 160,
"dfs": {
"radarDetected": false
}
},
"powerSettings": {
"transmitPower": 8,
"mode": "lpi"
}
}
]
< /code>
Hier ist das Skript, das ich verwende.  Wenn ich es ausführe, erhalte ich einen Fehler mit der Aufschrift: 'List' Objekt hat kein Attribut 'GET' 
:

Code: Select all

import requests
import json
import os

# --- Configuration ---
# It's recommended to store your API key as an [url=viewtopic.php?t=25360]environment[/url] variable for security
MERAKI_API_KEY = os.environ.get("MERAKI_DASHBOARD_API_KEY")
if not MERAKI_API_KEY:
print("Error: MERAKI_DASHBOARD_API_KEY [url=viewtopic.php?t=25360]environment[/url] variable not set.")
exit()

ORGANIZATION_ID = "YOUR_ORGANIZATION_ID"  # Replace with your Meraki Organization ID
BASE_URL = "https://api.meraki.com/api/v1"

HEADERS = {
"X-Cisco-Meraki-API-Key": MERAKI_API_KEY,
"Content-Type": "application/json",
}

def get_organization_devices(org_id):
"""Retrieves all devices in a given organization."""
url = f"{BASE_URL}/organizations/{org_id}/devices"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()  # Raise an exception for bad status codes
return response.json()

def get_device_wireless_status(network_id, serial):
"""Retrieves the wireless status for a specific device, including channel information."""
url = f"{BASE_URL}/devices/{serial}/wireless/radio/status"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
return response.json()

def main():
print(f"Retrieving devices for Organization ID: {ORGANIZATION_ID}...")
try:
devices = get_organization_devices(ORGANIZATION_ID)
access_points = [d for d in devices if d.get('productType') == 'wireless']

if not access_points:
print("No Meraki access points found in this organization.")
return

print(f"Found {len(access_points)} Meraki Access Points.")
for ap in access_points:
serial = ap.get('serial')
name = ap.get('name', 'N/A')
network_id = ap.get('networkId')

if not network_id:
print(f"Skipping AP {name} ({serial}): Not assigned to a network.")
continue

print(f"\n--- AP: {name} (Serial: {serial}) ---")
try:
wireless_status = get_device_wireless_status(network_id, serial)
bands = wireless_status.get('bands', {})
for band_name, band_data in bands.items():
channel = band_data.get('channel')
print(f"  Band: {band_name}, Current Channel: {channel}")
except requests.exceptions.RequestException as e:
print(f"  Error fetching wireless status for {serial}: {e}")

except requests.exceptions.RequestException as e:
print(f"Error communicating with Meraki API: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
main()
< /code>
Ich erhalte den folgenden Fehler beim Ausführen des Skripts: < /p>
Traceback (most recent call last):
File "xxxx", line 55, in main
bands = wireless_status.get('bands', {})

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post