I am facing a bit of a situation,
Scenario: I got a django rest api running on my localhost:8000 and I want to access the api using my command line. I have tried urllib2 and python requests libs to talk to the api but failed(i'm getting a 503 error). But when I pass google.com as the url, I am getting the expected response. So I believe my approach is correct but I'm doing something wrong. please see the code below :
import urllib, urllib2, httplib
url = 'http://localhost:8000'
httplib.HTTPConnection.debuglevel = 1
data = urllib.urlopen(url);
request = urllib2.Request(url)
opener = urllib2.build_opener()
feeddata = opener.open(request).read()
I had a similar problem, but found that it was the company's proxy that was preventing from pinging myself.
503 Reponse when trying to use python request on local website
>>> import requests >>> session = requests.Session() >>> session.trust_env = False >>> r = session.get("http://localhost:5000/") >>> r <Response > >>> r.content 'Hello World!'
If you are registering your serializers with DefaultRouter then your api will appear at
http://localhost:8000/api/ for an html view of the index http://localhost:8000/api/.json for a JSON view of the index http://localhost:8000/api/appname for an html view of the individual resource http://localhost:8000/api/appname/.json for a JSON view of the individual resource
you can check the response in your browser to make sure your URL is working as you expect.