Django Failed to establish a new connection <urllib3.connection.HTTPSConnection>

hello, I use Django 2.2 and I have an api that is connected to two Esx servers.

  • a = 10.131.171.80
  • b = 10.131.171.90

everything works perfectly, except that during my test when I add a third fake server, it tells me that the server does not respond because connection time out. So the error is handled and I don’t have a yellow page from Django.

Except when enter fourth server this time private ip like 172.16.15.15 I have an error Max retries exceeded NewConnectionError(<urllib3.connection.HTTPSConnection’>

But when I use a public IP like 1.1.1.1, I don’t get any error.

What I would like is to handle the error better when someone enters a private IP.

what’s weird is that on local env localhost everything works even with private ip but when I am in my test server it doesn’t work.

My full traceback
Here is my api.py file.

import requests
import os
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning


requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
os.environ['REQUESTS_CA_BUNDLE'] = os.path.join('/etc/ssl/certs/')
req = requests.Session()
req.verify = False


class VmwareApi:
    def __init__(self):
        self.ip = ""
        self.user = ""
        self.password = ""
        self.arg1 = ""
        self.ses = ""
        self.params = ""

    def session(self):
        try:
            a = req.post('https://' + self.ip + '/api/session',
                         auth=(self.user, self.password),
                         timeout=1, verify=False)
            self.ses = str(a.json())
        except requests.exceptions.Timeout:
            return 'ConnectTimeoutError'
        return req

    def param_loader(self):
        if self.params:
            self.params = json.loads(self.params)

    def vapirequestget(self):
        try:
            VmwareApi.param_loader(self)
            myreq = req.get('https://' + self.ip + self.arg1,
                            params=self.params, verify=False,
                            headers={"vmware-api-session-id": self.ses},
                            timeout=1)
            return myreq
        except requests.exceptions.Timeout:
            return 'ConnectTimeoutError'