External API Request with Proxy within Django

Viewed 423

I have a local proxy service up at 127.0.0.1:8080

I can make api calls with it but not within django, for some reason it works in regular python code (i.e. python test.py) but not in django.

import requests

proxies = {
    'http': 'http://127.0.0.1:8080',
    'https': 'https://127.0.0.1:8080',
}

response = requests.get('https://api.ipify.org?format=json', proxies=proxies)

django v3.0.8 and requests v2.24.0

I have an assumption that WSGI somehow override's my proxy settings

The error

HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa922093340>: Failed to establish a new connection: [Errno 111] Connection refused')))
1 Answers

For anyone that comes across this, this is a docker networking issue. 127.0.0.1 within a docker container references the internal docker network not your local environment's network. Use 172.17.0.1 to specify your local environment.

Related