You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python调用API获取Aruba Airwave报告?代码报错求助

问题排查:Aruba Airwave API登录并获取报告数据报错

需求

登录Aruba设备,通过Aruba Airwave的API端点获取报告数据。

现有Python代码

import xml.etree.ElementTree as ET # libxml2 and libxslt
import requests                    # HTTP requests
import csv
import sys

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------

# Login/password for Airwave (read-only account)
LOGIN = 'operator'
PASSWD = 'verylongpasswordforyourreadonlyaccount'

# URL for REST API
LOGIN_URL = 'https://aruba-airwave.example.com/LOGIN'
AP_LIST_URL = 'https://aruba-airwave.example.com/report_detail?id=100'

# Delimiter for CSV output
DELIMITER = ';'

# HTTP headers for each HTTP request
HEADERS = {
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Cache-Control' : 'no-cache'
}

# ---------------------------------------------------------------------------
# Fonctions
# ---------------------------------------------------------------------------

def open_session():
    """Open HTTPS session with login"""

    ampsession = requests.Session()
    data = 'credential_0={0}&credential_1={1}&destination=/&login=Log In'.format(LOGIN, PASSWD)
    loginamp = ampsession.post(LOGIN_URL, headers=HEADERS, data=data)
    return {'session' : ampsession, 'login' : loginamp}

def get_ap_list(session):
      output=session.get(AP_LIST_URL, headers=HEADERS, verify=False)
      ap_list_output=output.content
      return ap_list_output

def main():
   session=open_session()
   get_ap_list(session)
main()

运行报错

get() takes no keyword arguments

问题原因与修复

问题根源

open_session函数返回的是包含session和login的字典,但get_ap_list函数直接对该字典调用get()方法——字典的get()仅用于按键取值,不支持headers、verify这类关键字参数,因此触发报错。

修复方案

调整代码,确保传递给get_ap_list的是真正的requests.Session对象,而非字典。以下是两种可行修改方式:

方式1:修改main函数,正确传递Session对象

def main():
    # 获取包含Session的字典
    session_data = open_session()
    # 取出requests.Session对象传入get_ap_list
    ap_list_content = get_ap_list(session_data['session'])
    # 可添加后续处理逻辑,比如解析XML并写入CSV
    print(ap_list_content)

方式2:修改open_session函数,直接返回Session对象(无需保留login响应时使用)

def open_session():
    """Open HTTPS session with login"""
    ampsession = requests.Session()
    data = 'credential_0={0}&credential_1={1}&destination=/&login=Log In'.format(LOGIN, PASSWD)
    ampsession.post(LOGIN_URL, headers=HEADERS, data=data)
    # 直接返回requests.Session实例
    return ampsession

def main():
    session = open_session()
    ap_list_content = get_ap_list(session)
    # 示例:解析XML并导出为CSV(需根据实际返回的XML结构调整节点路径)
    root = ET.fromstring(ap_list_content)
    with open('ap_report.csv', 'w', newline='', encoding='utf-8') as csv_file:
        writer = csv.writer(csv_file, delimiter=DELIMITER)
        # 写入表头
        writer.writerow(['AP名称', 'IP地址', '状态'])
        # 遍历XML节点提取数据
        for ap_node in root.findall('.//access_point'):  # 替换为实际的XML节点路径
            ap_name = ap_node.find('name').text if ap_node.find('name') is not None else '未知'
            ap_ip = ap_node.find('ip_address').text if ap_node.find('ip_address') is not None else '未知'
            ap_status = ap_node.find('status').text if ap_node.find('status') is not None else '未知'
            writer.writerow([ap_name, ap_ip, ap_status])

额外注意事项

  • verify=False会跳过SSL证书验证,生产环境建议配置可信CA证书路径,避免安全风险。
  • 需根据Airwave API返回的实际XML结构,调整解析代码中的节点路径,确保正确提取所需数据。

内容的提问来源于stack exchange,提问作者user1471980

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 04:35:03