当前位置: 动力学知识库 > 问答 > 编程问答 >

django - Python requests 503 erros when trying to access localhost:8000

问题描述:

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

网友答案:

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!'
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: