TransWikia.com

Vertic API authentications issues via Python using Requests package

Stack Overflow Asked by mongotop on November 12, 2021

Trying to connect to Vertiv API to get info regarding PDU data via python using the code bellow, but I keep getting this error. the IP address and logins info are accurate because I was able to connect via the GUI. so I have to set something inside the application for requests to be able to connect?

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

This is the error message I receive.

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

One Answer

I believe, sending of headers won't make any difference because there is no data that is being sent with this request.
try refactoring your request like this:

credentials = {
    "username": "access_username",
    "password": "user_password",
}


response = requests.post(
    final_url,
    auth=(credentials["username"], credentials["password"]),
)

Answered by Vishal Singh on November 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP