尝试访问localhost时,Python会请求503错误:8000 [英] Python requests 503 erros when trying to access localhost:8000
问题描述
我面临一些情况,
情景:我在本地主机上运行了一个django rest api:8000,我想使用我的命令行。我已经尝试urllib2和python请求libs与api谈话,但失败(我得到一个503错误)。但是当我通过google.com作为URL时,我得到了预期的回应。所以我相信我的做法是正确的,但我做错了事情。请参阅以下代码:
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
谢谢
推荐答案
一个类似的问题,但发现这是该公司的代理人阻止自己ping。
I had a similar problem, but found that it was the company's proxy that was preventing from pinging myself.
尝试:
>>> import requests
>>> session = requests.Session()
>>> session.trust_env = False
>>> r = session.get("http://localhost:5000/")
>>> r
<Response [200]>
>>> r.content
'Hello World!'
这篇关于尝试访问localhost时,Python会请求503错误:8000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!