'requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /api/1/
I am trying to communicate between Django and Python file but I am getting below error :
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /api/1/ (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
I have created a Python file named test.py In a Django app and trying to communicate between them. My Python file contains below code :
import requests
BASE_URL='http://127.0.0.1:8000/'
ENDPOINT='api/'
def get_resource(id):
resp=requests.get(BASE_URL+ENDPOINT+id+'/')
print(resp.status_code)
print(resp.json())
id=input("Enter some ID: ")
get_resource(id)
Models.py contains:-
from django.db import models
# Create your models here.
class Employee(models.Model):
eno=models.IntegerField()
ename=models.CharField(max_length=70)
esal=models.FloatField()
eaddr=models.CharField(max_length=100)
Admin.py contains:-
from django.contrib import admin
from testapp.models import Employee
# Register your models here.
class EmployeeAdmin(admin.ModelAdmin):
list_display= ['id','eno','ename','esal','eaddr']
admin.site.register(Employee,EmployeeAdmin)
My Views.py contains:-
from django.shortcuts import render
from django.views.generic import View
from testapp.models import Employee
import json
from django.http import HttpResponse
class EmployeeDetailCBV(View):
def get(self,request,id,*args,**kwargs):
emp = Employee.objects.get(id=id)
emp_data = {'eno':emp.eno , 'ename':emp.ename , 'esal':emp.esal , 'eaddr':emp.eaddr}
json_data=json.dumps(emp_data)
return HttpResponse(json_data , content_type='application/json')
urls.py file contains:-
from django.contrib import admin
from django.urls import path
from testapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('api/(?P<id>\d+)/$', views.EmployeeDetailCBV.as_view()),
]
Getting error like :
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /api/1/ (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
Please help me. Thank you techies....
Solution 1:[1]
I got the real reason behind the failure. I had run only py test.py from only 1 terminal to access the python file. At that time the server was not running so commmunication couldn't happen. I then opened a separate terminal and ran the Dev server and now my Python file is able to communicate with Django file. Now its running fine.
Solution 2:[2]
Try this :
import requests
# BASE_URL='http://127.0.0.8000' <<< Url with port malformed
BASE_URL='http://127.0.0.1:8000'
ENDPOINT='api/'
def get_resource():
# resp=requests.get(BASE_URL+ENDPOINT) <<< Request url malformed
resp=requests.get(BASE_URL+"/"+ENDPOINT)
print(resp.status_code)
print(resp.json())
get_resource()
Solution 3:[3]
You just need to open separate terminal and run your python code and
make sure your server is running at that time
Solution 4:[4]
Faced similar problem, changing port number solved the issue.
Solution 5:[5]
please check your port once in my case I had port issue I have used port 8525, instead of browsing 8525 I have browsed with 8000
Solution 6:[6]
To launch your code locally, use your fixed IPv4 address.
For Windows users: open a command prompt (cmd) and run the ipconfig command, retrieve the IP@ which is at IPv4 address. . . . . . . . . . . . . .: it is generally of type 192.168.x.x
IPv4 address in command prompt
then go to your settings.py and add: ALLOWED_HOSTS = ['*']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Learner |
| Solution 2 | Arkistarvh Kltzuonstev |
| Solution 3 | Manas Baral |
| Solution 4 | user3644678 |
| Solution 5 | Naresh Chary |
| Solution 6 | olorunshola matins |
