How do I resolve HTTPSConnectionPool error, Max retries exceeded with url in python?

requests didn't work as expected

    def post(self, request):
        base_url = os.environ.get('AUTH_URL', 'http://localhost')
        # Make a POST request to the authentication endpoint with the payload

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                          "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
            'accept': 'application/json',
            'Content-Type': 'application/json',
        }

        json_data = {
            'username': os.environ.get("GARAJ_USERNAME", "admin"),
            'password': os.environ.get("GARAJ_PASSWORD", "admin123"),
        }
        response = requests.post(base_url + '/api/token/', json=json_data, headers=headers)

        # Check the response status code
        if response.status_code == 200:
            data = response.json()
            # Extract the JWT token from the response
            jwt_token_access = data['access']
            # Use the JWT token for subsequent API requests
            headers['Authorization'] = f'Bearer {jwt_token_access}'
            auth_url = base_url + '/api/boshqarma/avto/'
            response = requests.get(auth_url, headers=headers)

            for x in response.json()['results']:
                if x["davlat_raqami"] == request.data['avto_raqami']:
                    boshqarma = x["boshqarma"]
                    response = requests.get(base_url + f'/api/boshqarma/boshqarma/{boshqarma}/', headers=headers)
                    tuman = response.json()['tuman']
                    response = requests.get(base_url + f'/api/boshqarma/bolim/{tuman}/', headers=headers)
                    tuman = response.json()['tuman']
                    tuman = Tuman.objects.get(tuman_nomi=tuman)
                    data = {'tuman_id': tuman.id}
                    response = Response(data)
                    return response
            else:
                return Response("Xato avto raqam")

            # Make additional API requests with the headers containing the JWT token

        else:
            return Response(response)

This code block works on using localhost but did't work when it is on server,but returns below error.I manage both servers client and host server too. ConnectionError at /api/check-car/ HTTPSConnectionPool(host='example.uz', port=443): Max retries exceeded with url: /api/token/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f3d48892f50>: Failed to establish a new connection: [Errno 111] Connection refused'))

Upvotes: 0

Views: 4236

Answers (1)

So, basically my both services client and host were running on the same server on different subdomains, using http instead of https and adding host's local IP address to client's /etc/hosts and binding it to host subdomain resolved the issue.

Upvotes: 0

Related Questions