Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

python - HTTPConnectionPool: Max retries exceeded Caused by NewConnectionError: Failed to establish a new connection: [Errno 13] Permission denied

Hi I'm trying to get REST request from a certain URL and if I input the url directly in the browser it works ok and output is as show in the image below

rest request via browser

If I also try to query the url via native python on the same server where my django app is and it works fine too.

rest requests via python

The problem is when i put the request in view.py in my django project I get a connection error as the the title of this question.

import requests

def my_method(request):
    response = requests.get('http://10.150.20.103:8000/se/se-seid')
    special_events_dashboard_events = response.json()
    for event in special_events_dashboard_events['events']:
        context['special_events'].append(event)

    return render(request, 'updates/updates_network.html', context)

When I check the page in the browser, I get this error.

django error

The REST API url is of different IP Address from my django application.

Is this a setting problem in my django application? How to work around this? I have been searching in the internet but I cant seem to find an answer to this.

question from:https://stackoverflow.com/questions/65840232/httpconnectionpool-max-retries-exceeded-caused-by-newconnectionerror-failed-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the answer to the problem here. It turns out its not django that is causing the problem rather its the Selinux settings of the server where my django project is deployed.

I found this similar permission error problem here, where the solutions states to enable httpd_can_network_connect context. After setting the context to ON, i can now connect to the api url.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...