0

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

print "urllib"

data = urllib.urlopen(url);

print "urllib2"

request = urllib2.Request(url)
opener = urllib2.build_opener()
feeddata = opener.open(request).read()

print "End\n"

Envioroments:

OS Win7
python v2.7.5
Django==1.6
Markdown==2.3.1
colorconsole==0.6
django-filter==0.7
django-ping==0.2.0
djangorestframework==2.3.10
httplib2==0.8
ipython==1.0.0
jenkinsapi==0.2.14
names==0.3.0
phonenumbers==5.8b1
requests==2.1.0
simplejson==3.3.1
termcolor==1.1.0
virtualenv==1.10.1

Thanks

Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72

2 Answers2

5

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

Try:

>>> import requests
>>> session = requests.Session()
>>> session.trust_env = False
>>> r = session.get("http://localhost:5000/")
>>> r
<Response [200]>
>>> r.content
'Hello World!'
Community
  • 1
  • 1
Kenny Ho
  • 383
  • 1
  • 5
  • 12
0

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.

rayjay
  • 359
  • 3
  • 16
  • Yes, I get the correct response when I use the web browser. and I have tried using curl as django expains on the tutorial, can't seem to talk to the api using curl as well – Harshana Nanayakkara Dec 30 '13 at 09:46
  • I just checked and `curl` works for me. `curl localhost:8000/api/.json` gives me the same text as my browser. I assume you are running `manage.py runserver` at the same time in a different session? And you are running `curl` on the same computer, or telling `runserver` to accept `LAN` connections if it's another computer? – rayjay Dec 30 '13 at 10:53
  • Actually, I have to goto the following links to get what you mentioned above.
    http://localhost:8000/Jobs/ for an html view of the index
    http://localhost:8000/Jobs/?format=json json for a JSON view of the index
    http://localhost:8000/Jobs/1/?format=json for a JSON view of the individual resource
    could this be why I'm having issues
    – Harshana Nanayakkara Dec 30 '13 at 20:08
  • I'd put the root of my api urls at `localhost:8000/api/` so you'd then be at `/api/jobs' for your REST interface and `/job` is free for your normal html views, but that's just a design choice, I don't see that it would affect what you're doing. – rayjay Dec 31 '13 at 05:23
  • You didn't say if your python shell is on the same computer as your Django server. What's your setup? Is everything running on a desktop machine: `django`, `browser` and `python shell`? `curl` should be making the same requests as your browser if it's running on the same machine and your typing the same url. If `localhost:8000/Jobs` works in the browser then `curl localhost:8000/Jobs` should work at the terminal on the same PC as the browser is running on. If you're accessing from a different PC on the same `subdomain` (same `LAN`), you need `python manage.py runserver 0.0.0.0:8000` – rayjay Dec 31 '13 at 05:42
  • If your `django server` is on a `server` machine and your `python` shell on another then you could have `firewall` issues as well - you would need to open `port 8000` – rayjay Dec 31 '13 at 05:45
  • yup, At the moment every thing is running on my local pc. – Harshana Nanayakkara Jan 05 '14 at 21:47
  • Just tried http from a VM via a browser, I cant seem to do that. However I have a Jenhkins on Localhost:8080, I cant http to that with no problem. – Harshana Nanayakkara Jan 07 '14 at 21:59
  • I'm afraid it sounds windows specific. Maybe a firewall issue, or maybe you need to run your command shell as administrator. – rayjay Jan 08 '14 at 18:38
  • You could try setting up apache with mod_wsgi to see if that will respond properly on port 80. That's not locating the problem necessarily but trying a completely separate hosting solution that may avoid whatever is stopping your development server from working.Sorry I couldn't help you find the fault :( Ray. – rayjay Jan 08 '14 at 18:40
  • Found what was going on, we have an internal software was interfering with the port. So when I tried on my home pc it worked perfectly. – Harshana Nanayakkara Jan 12 '14 at 22:02