请求失败时登录到 Aruba 的 NBAPI for Mobility Master
Logging in to Aruba's NBAPI for Mobility Master with requests failing
我正在使用 requests
呼叫 Aruba 的 NBAPI 作为移动主机。他们的文档很少,只有 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
方法不正确。
您提出的请求不同。 curl
的 -d
开关发送 POST 数据,而不是授权 header。来自 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
格式的请求 body:
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
command-line 示例的 API 文档取决于对该工具的最低限度的熟悉程度,始终值得阅读 curl
manpage 以了解内容交换机可以,并且对 HTTP headers 和类似的东西有最低限度的了解。
如有疑问,请同时使用 curl
和 requests
将您的请求发送到 httpbin.org test site, and compare the results. Here, using the /anything
endpoint 并使用 curl
给出:
$ 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"
}
这应该能更清楚地说明这里的区别。
我正在使用 requests
呼叫 Aruba 的 NBAPI 作为移动主机。他们的文档很少,只有 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
方法不正确。
您提出的请求不同。 curl
的 -d
开关发送 POST 数据,而不是授权 header。来自 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
格式的请求 body:
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
command-line 示例的 API 文档取决于对该工具的最低限度的熟悉程度,始终值得阅读 curl
manpage 以了解内容交换机可以,并且对 HTTP headers 和类似的东西有最低限度的了解。
如有疑问,请同时使用 curl
和 requests
将您的请求发送到 httpbin.org test site, and compare the results. Here, using the /anything
endpoint 并使用 curl
给出:
$ 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"
}
这应该能更清楚地说明这里的区别。