python-3.x - 使用 Requests 包通过 Python 的 Vertic API 身份验证问题
问题描述
尝试使用以下代码连接到 Vertiv API 以通过 python 获取有关 PDU 数据的信息,但我不断收到此错误。IP 地址和登录信息是准确的,因为我能够通过 GUI 进行连接。所以我必须在应用程序中设置一些东西才能请求能够连接?
import requests
import json
##### Section: Authenticate to the API
# Set the base URL for the API call
base = 'http://15.10.10.100:8080/api/v1' #This is the URL to an ACS devices API
# Define the final portion of the URL for authenticating and getting an auth token
sessionlogin = '/sessions/login'
# Build the final URL to send
final_url = base + sessionlogin
# Set the POST parameters
headers = {'Content-Type': 'application/json'} #Have to set it to JSON
credentials = {'username': 'access_username', 'password': 'user_password'} #Default username and password for an ACS device.
# Send the POST and the parameters
response = requests.post(final_url, data=json.dumps(credentials), headers=headers) #Sending the POST authentication
这是我收到的错误消息。
raceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "/usr/local/lib/python3.6/site-packages/urllib3/util/connection.py", line 84, in create_connection
raise err
File "/usr/local/lib/python3.6/site-packages/urllib3/util/connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 677, in urlopen
chunked=chunked,
File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib64/python3.6/http/client.py", line 1254, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib64/python3.6/http/client.py", line 1300, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib64/python3.6/http/client.py", line 1249, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib64/python3.6/http/client.py", line 1036, in _send_output
self.send(msg)
File "/usr/lib64/python3.6/http/client.py", line 974, in send
self.connect()
File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 187, in connect
conn = self._new_conn()
File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 172, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f535f6b1a90>:
Failed to establish a new connection: [Errno 111] Connection refused
解决方案
我相信,发送headers
不会有任何区别,因为这个请求没有发送任何数据。
尝试像这样重构您的请求:
凭据 = { “用户名”:“访问用户名”, “密码”:“用户密码”, } 响应 = 请求.post( 最终网址, auth=(credentials["username"], credentials["password"]) , )
推荐阅读
- java - 根据 OIM 中的自定义属性值搜索用户
- c++ - 将整数变量地址分配给类指针
- java - 我的应用程序中的 Paytm 集成
- django - 可以在 Django 中为 slack slash command api mad 禁用 CSRF 吗?
- android - 用于数据发布的 react-native 中的表与 Listview
- java - 在 Spring Maven 项目中添加 web.xml
- java - 反射 - 使用 Long 和 Number 参数调用方法
- php - 如何在循环中访问 ["val1","val2","val3"] 等数组中的 PHP 值
- javascript - 点击时输入关闭列表,但不应该
- kotlin - 如何为 Kotlin 实施 applyif?