【发布时间】:2020-07-13 06:38:32
【问题描述】:
尝试使用以下代码连接到 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
【问题讨论】:
-
您的环境是否可以通过
curl访问该网站? -
是的。我运行以下 cmd
curl -H "Content-Type:application/json" -H "Accept:application/json" http://15.10.10.100/api/v1/sessions/login -d '{"username":"access_username","pasword":"user_password"}'并没有得到任何输出或错误消息 -
您确定 API 接受凭据作为正文吗?如果是这样,那么试试
requests.post(final_url, json=credentials, headers=headers) -
就
curl命令而言,尝试发送用户名和密码如下 -curl --user access_username: user_password http://15.10.10.100/api/v1/sessions/login
标签: python-3.x api authentication python-requests