我面临一些情况,
场景:我在我的localhost:8000上运行了django rest api,我希望使用我的命令行访问api。我已经尝试了urllib2和python请求libs与api交谈但失败了(我收到503错误)。但是当我通过google.com作为网址时,我得到了预期的回复。所以我相信我的方法是正确的,但我做错了。请参阅以下代码:
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
谢谢
答案 0 :(得分:5)
我遇到了类似的问题,但发现该公司的代理人阻止了我自己的ping。
503 Reponse when trying to use python request on local website
尝试:
public String[] listAssets(String path) throws IOException {
//return getAssets().list(path);
try {
JSONArray assets = fileTree.getJSONArray("children");
if (path.equals("")) { //asking for root dir
String[] names = new String[assets.length()];
for(int i = 0; i < assets.length(); i++){
names[i] = assets.getJSONObject(i).getString("name");
}
return names;
}
else { //same subdirectory
String[] parts = path.split(Pattern.quote("/"));
JSONArray array = findSubdirectory(parts, 0, assets);
String[] names = new String[array.length()];
for(int i = 0; i < array.length(); i++){
names[i] = array.getJSONObject(i).getString("name");
}
return names;
}
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
答案 1 :(得分:0)
如果您使用DefaultRouter注册序列化程序,那么您的api将出现在
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
您可以在浏览器中查看回复,以确保您的网址正常运行。