在请求失败的情况下登录到Aruba的NBAPI for Mobility Master

2024-04-25 11:40:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用requests调用Aruba的NBAPI以获得mobility master。他们的文档很少有Python示例。API需要一个初始身份验证来获取需要在每个get请求中引用的UID。我无法让登录在Python中工作。你知道吗

我使用的GET请求可能是问题的一部分,但我对Aruba提供的curl示例的理解是使用默认的GET方法。请注意,我也没有验证SSL证书,因为我的最终目标是零接触配置。你知道吗

下面是他们提供给auth的curl命令

curl --insecure -c "aruba-cookie-jar" -d "username=username&password=password" https://<url-here>:4343/v1/api/login

命令输出:

{"_global_result": {"status":"0", "status_str": "You've logged in successfully.", "UIDARUBA":"<key output here>"}}

我尝试使用“requests”将其转换为python,如下所示

import requests

session = requests.Session()
session.verify = False
r = session.get('https://<url-here>:4343/v1/api/login', auth=('username', 'password'))

我在检查响应时得到以下信息(ipython)

In [6]: r.status_code
Out[6]: 401
In [7]: print(r.text)
{"_global_result": {"status":"1", "status_str": "Unauthorized request, authentication failed"}}

这个请求我做错了什么?在Python中使用POST方法时,会产生相同的输出。我认为在Python示例中使用的auth方法是不正确的。你知道吗


Tags: 方法https命令auth示例getheresession
1条回答
网友
1楼 · 发布于 2024-04-25 11:40:27

你没有提出同样的要求。用于curl-d开关发送POST数据,而不是授权头。从curl文档中:

-d, data <data>

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

您使用的是GET请求,而不是POST。在data参数中发送用户名和密码,您可以在此处使用字典将requests句柄编码为application/x-www-form-urlencoded格式的请求正文:

session = requests.Session()
session.verify = False
login_info = {'username': 'username', 'password': 'password'}
r = session.post('https://<url-here>:4343/v1/api/login', data=login_info)

一般来说,使用curl命令行示例的API文档依赖于对该工具的最低限度的熟悉,阅读^{} manpage以了解开关的作用,以及对HTTP头等的最低限度的了解总是值得的。你知道吗

如果有疑问,请同时使用curlrequests将请求发送到httpbin.org test site,并比较结果。在这里,将^{} endpointcurl一起使用可以得到:

$ curl  insecure -c "aruba-cookie-jar" -d "username=username&password=password" https://httpbin.org/anything
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "password": "password",
    "username": "username"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "35",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "POST",
  "origin": "...",
  "url": "https://httpbin.org/anything"
}

request代码输出时:

>>> import requests
>>> session.verify = False
>>> r = session.get('https://httpbin.org/anything', auth=('username', 'password'))
/.../lib/python3.8/site-packages/urllib3/connectionpool.py:842: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn((
>>> print(r.text)
{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.21.0"
  },
  "json": null,
  "method": "GET",
  "origin": "...",
  "url": "https://httpbin.org/anything"
}

希望这能让我们更清楚这里的区别在哪里。你知道吗

相关问题 更多 >