gpt4 book ai didi

python-3.x - Python - 通过 SOAP 查询 Solarwinds N-Central 时出错

转载 作者:行者123 更新时间:2023-12-05 07:39:59 28 4
gpt4 key购买 nike

我正在使用 Python 3 编写脚本,为 Solarwinds N-Central 生成客户报告。该脚本使用 SOAP 来查询 N-Central,我正在为这个项目使用 zeep。虽然不是 python 的新手,但我是 SOAP 的新手。

当调用 CustomerList 函数时,我得到了 TypeError: __init__() got an unexpected keyword argument 'listSOs'

import zeep

wsdl = 'http://' + <server url> + '/dms/services/ServerEI?wsdl'
client = zeep.CachingClient(wsdl=wsdl)

config = {'listSOs': 'true'}

customers = client.service.CustomerList(Username=nc_user, Password=nc_pass, Settings=config)

根据下面的参数,“listSOs”不仅是一个有效关键字,而且是唯一被接受的关键字。

客户名单

public com.nable.nobj.ei.Customer[] CustomerList(String username, String password, com.nable.nobj.ei.T_KeyPair[] settings) throws RemoteException

参数:

  • 用户名 - MSP N-central 用户名
  • 密码-对应的MSP N-central密码
  • settings - 存储在 T_KeyPair[] 中的非默认设置列表。下面是可接受的键和值的列表。如果不使用则留空
    • (键)listSOs -(值)“真”或“假”。如果为真,则仅显示 SO,如果为假,则仅显示客户和站点。默认值为 false。

我也试过将字典作为列表的一部分传递:

config = []
key = {'listSOs': 'true'}
config += key

TypeError: Any element received object of type 'str', expected lxml.etree._Element or builtins.dict or zeep.objects.T_KeyPair

完全省略设置值:

customers = client.service.CustomerList(Username=nc_user, Password=nc_pass)

zeep.exceptions.ValidationError: Missing element Settings (CustomerList.Settings)

并尝试使用 zeep 的 SkipValue:

customers = client.service.CustomerList(Username=nc_user, Password=nc_pass, Settings=zeep.xsd.SkipValue)

zeep.exceptions.Fault: java.lang.NullPointerException

我可能遗漏了一些简单的东西,但我一直在用头撞墙,为此我希望有人能给我指出正确的方向。

最佳答案

这是我的 getAssets.py 脚本的源代码。我是用 Python2.7 做的,虽然很容易升级。希望对其他人有帮助,N-central 的 API 文档真的很糟糕哈哈。

#pip2.7 install zeep
import zeep, sys, csv, copy
from zeep import helpers

api_username = 'your_ncentral_api_user'
api_password='your_ncentral_api_user_pw'

wsdl = 'https://(yourdomain|tenant)/dms2/services2/ServerEI2?wsdl'
client = zeep.CachingClient(wsdl=wsdl)

response = client.service.deviceList(
username=api_username,
password=api_password,
settings=
{
'key': 'customerId',
'value': 1
}
)

# If you can't tell yet, I code sloppy

devices_list = []
device_dict = {}
dev_inc = 0
max_dict_keys = 0
final_keys = []

for device in response:
# Iterate through all device nodes
for device_properties in device.items:
# Iterate through each device's properties and add it to a dict (keyed array)
device_dict[device_properties.first]=device_properties.second

# Dig further into device properties
device_properties = client.service.devicePropertyList(
username=api_username,
password=api_password,
deviceIDs=device_dict['device.deviceid'],
reverseOrder=False
)

prop_ind = 0 # This is a hacky thing I did to make my CSV writing work
for device_node in device_properties:
for prop_tree in device_node.properties:
for key, value in helpers.serialize_object(prop_tree).items():
prop_ind+=1
device_dict["prop" + str(prop_ind) + "_" + str(key)]=str(value)

# Append the dict to a list (array), giving us a multi dimensional array, you need to do deep copy, as .copy will act like a pointer
devices_list.append(copy.deepcopy(device_dict))

# check to see the amount of keys in the last item
if len(devices_list[-1].keys()) > max_dict_keys:
max_dict_keys = len(devices_list[-1].keys())
final_keys = devices_list[-1].keys()

print "Gathered all the datas of N-central devices count: ",len(devices_list)

# Write the data out to a CSV
with open('output.csv', 'w') as csvfile:
fieldnames = final_keys
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

writer.writeheader()

for csv_line in devices_list:
writer.writerow(csv_line)

关于python-3.x - Python - 通过 SOAP 查询 Solarwinds N-Central 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574837/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com