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 Pytho
n 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....
To solve the requests "ConnectionError: Max retries exceeded with url", use a Retry object and specify how many connection-related errors to retry on and set a backoff factor to apply between attempts.
Error Exception ConnectionError − This will be raised, if there is any connection error. For example, the network failed, DNS error so the Request library will raise ConnectionError exception. Response. raise_for_status() − Based on status code i.e. 401, 404 it will raise HTTPError for the url requested.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With