简体   繁体   English

使用Python进行AppDynamics REST调用的问题

[英]Issue with AppDynamics REST call with Python

I tried to call AppDynamics API using python requests but face an issue. 我尝试使用python请求调用AppDynamics API,但遇到了问题。 I wrote a sample code using the python client as follows... 我使用python客户端编写了示例代码,如下所示...

from appd.request import AppDynamicsClient
c = AppDynamicsClient('URL','group','appd@123')
for app in c.get_applications():
    print app.id, app.name

It works fine. 工作正常。

But if I do a simple call like the following 但是如果我像下面这样简单地打电话

import requests
usr =<uid>
pwd =<pwd>
url ='http://10.201.51.40:8090/controller/rest/applications?output=JSON'
response = requests.get(url,auth=(usr,pwd))
print 'response',response

I get the following response: 我得到以下回应:

response <Response [401]>

Am I doing anything wrong here ? 我在这里做错什么了吗?

Couple of things: 几件事情:

I think the general URL format for app dynamics applications are (notice the '#'): 我认为应用程式动态应用程式的一般网址格式为(请注意「#」):

url ='http://10.201.51.40:8090/controller/#/rest/applications?output=JSON'

Also, I think the requests.get method needs an additional parameter for the 'account'. 另外,我认为request.get方法需要为“帐户”添加一个附加参数。 For instance, my auth format looks like: 例如,我的身份验证格式如下:

auth = (_username + '@' + _account, _password)

I am able to get a right response code back with this config. 我可以使用此配置获得正确的响应代码。 Let me know if this works for you. 让我知道这是否适合您。

You could also use native python code for more control: 您还可以使用本机python代码进行更多控制:

example: 例:

import os
import sys
import urllib2
import base64

# if you have a proxy else comment out this line
proxy = urllib2.ProxyHandler({'https': 'proxy:port'}) 

opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

username = "YOUR APPD REST API USER NAME"
password = "YOUR APPD REST API PASSWORD"

#Enter your request
request = urllib2.Request("https://yourappdendpoint/controller/rest/applications/141/events?time-range-type=BEFORE_NOW&duration-in-mins=5&event-types=ERROR,APPLICATION_ERROR,DIAGNOSTIC_SESSION&severities=ERROR")

base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)

html = response.read()

This will get you the response and you can parse the XML as needed. 这将为您提供响应,并且您可以根据需要解析XML。

If you prefer it in JSON simply specify it in the request. 如果您更喜欢JSON,只需在请求中指定即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM