Python 3.x 使用请求包通过Python解决Vertic API身份验证问题

Python 3.x 使用请求包通过Python解决Vertic API身份验证问题,python-3.x,api,authentication,python-requests,Python 3.x,Api,Authentication,Python Requests,尝试使用下面的代码通过python连接到Vertiv API以获取有关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/ap

尝试使用下面的代码通过python连接到Vertiv API以获取有关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
raceback(最近一次通话最后一次):
文件“/usr/local/lib/python3.6/site packages/urllib3/connection.py”,第160行,位于康涅狄格州的新州
(self.\u dns\u主机,self.port),self.timeout,**额外\u千瓦
文件“/usr/local/lib/python3.6/site packages/urllib3/util/connection.py”,第84行,在create_connection中
提出错误
文件“/usr/local/lib/python3.6/site packages/urllib3/util/connection.py”,第74行,在create_connection中
sock.connect(sa)
ConnectionRefusedError:[Errno 111]连接被拒绝
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.6/site packages/urllib3/connectionpool.py”,urlopen中的第677行
分块的,
文件“/usr/local/lib/python3.6/site packages/urllib3/connectionpool.py”,第392行,在请求中
conn.request(方法,url,**httplib\u request\u kw)
请求中的文件“/usr/lib64/python3.6/http/client.py”,第1254行
self.\u发送\u请求(方法、url、正文、标题、编码\u分块)
文件“/usr/lib64/python3.6/http/client.py”,第1300行,在发送请求中
self.endheaders(body,encode\u chunked=encode\u chunked)
文件“/usr/lib64/python3.6/http/client.py”,第1249行,在endheaders中
self.\u发送\u输出(消息体,encode\u chunked=encode\u chunked)
文件“/usr/lib64/python3.6/http/client.py”,第1036行,在发送输出中
self.send(msg)
文件“/usr/lib64/python3.6/http/client.py”,第974行,在send中
self.connect()
文件“/usr/local/lib/python3.6/site packages/urllib3/connection.py”,第187行,在connect中
conn=自我。_new_conn()
文件“/usr/local/lib/python3.6/site packages/urllib3/connection.py”,第172行,位于康涅狄格州的新州
self,“无法建立新连接:%s”%e
urllib3.exceptions.NewConnectionError::
无法建立新连接:[Errno 111]连接被拒绝

我相信,发送
标题不会有任何区别,因为没有与此请求一起发送的数据。
尝试如下重构您的请求:

credentials = { "username": "access_username", "password": "user_password", } response = requests.post( final_url, auth=(credentials["username"], credentials["password"]), ) 凭据={ “用户名”:“访问用户名”, “密码”:“用户密码”, } response=requests.post( 最终url, 身份验证=(凭据[“用户名”]、凭据[“密码”]),
) 我相信,发送
标题
不会有任何区别,因为没有与此请求一起发送的数据。
尝试如下重构您的请求:

credentials = { "username": "access_username", "password": "user_password", } response = requests.post( final_url, auth=(credentials["username"], credentials["password"]), ) 凭据={ “用户名”:“访问用户名”, “密码”:“用户密码”, } response=requests.post( 最终url, 身份验证=(凭据[“用户名”]、凭据[“密码”]),
)您的环境是否可以通过
curl
访问该网站?可以。我运行以下cmd
curl-H“Content-Type:application/json”-H“Accept:application/json”http://15.10.10.100/api/v1/sessions/login -d'{“username”:“access\u username”,“pasword”:“user\u password”}'
并且我没有收到任何输出或错误消息您确定API接受凭据作为主体吗?如果是这样,请尝试
requests.post(最终url,json=credentials,headers=headers)
关于
curl
命令,请尝试按如下方式发送用户名和密码-
curl--user access\u username:user\u passwordhttp://15.10.10.100/api/v1/sessions/login
您的环境是否可以通过
curl
访问该网站?可以。我运行以下cmd
curl-H“Content-Type:application/json”-H“Accept:application/json”http://15.10.10.100/api/v1/sessions/login -d'{“username”:“access\u username”,“pasword”:“user\u password”}'
并且我没有收到任何输出或错误消息您确定API接受凭据作为主体吗?如果是这样,请尝试
requests.post(final\u url,json=credentials,headers=headers)
curl
命令而言,尝试按如下方式发送用户名和密码-
curl--user access\u用户名:user\u密码http://15.10.10.100/api/v1/sessions/login