一、API-json格式 (应用程序接口)


1、添加url:hostinfo/getjson/

[root@133simplecmdb-json]#vim/opt/python/django/simplecmdb-json/simplecmdb/urls.pyfromdjango.conf.urlsimportpatterns,include,urlfromdjango.contribimportadminadmin.autodiscover()urlpatterns=patterns('',#Examples:#url(r'^$','simplecmdb.views.home',name='home'),#url(r'^blog/',include('blog.urls')),url(r'^admin/',include(admin.site.urls)),url(r'^hostinfo/collect/$','hostinfo.views.collect'),url(r'^hostinfo/getjson/$','hostinfo.views.getjson'),)


2、在视图里面定义函数

[root@133simplecmdb-json]#vim/opt/python/django/simplecmdb-json/hostinfo/views.pyfromhostinfo.modelsimportHost,HostGroup#需要倒入HostGroup#增加一个函数,defgetjson(req):ret_list=[]hg=HostGroup.objects.all()forginhg:ret={'groupname':g.groupname,'members':[]}forhing.members.all():ret_h={'hostname':h.hostname,'ip':h.ip}ret['members'].append(ret_h)ret_list.append(ret)returnHttpResponse(json.dumps(ret_list))

3、web访问:http://112.65.140.13:8080/hostinfo/getjson/得到Json数据:

[root@133simplecmdb-json]#curlhttp://112.65.140.133:8080/hostinfo/getjson/[{"groupname":"group1","members":[{"ip":"112.65.140.132","hostname":"localhost.localdomain"},{"ip":"192.168.1.168","hostname":"test"}]},{"groupname":"group2","members":[]}]In[5]:importurllib,urllib2In[6]:urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getjson/')Out[6]:<addinfourlat33716920whosefp=<socket._fileobjectobjectat0x1fc33d0>>In[7]:req=urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getjson/')In[8]:req.read()Out[8]:'[{"groupname":"group1","members":[{"ip":"112.65.140.132","hostname":"localhost.localdomain"},{"ip":"192.168.1.168","hostname":"test"}]},{"groupname":"group2","members":[]}]'In[9]:req.read()Out[9]:''

In[1]:importjsonIn[3]:req=urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getjson/')In[4]:data=req.read()In[11]:d=json.loads(data)In[12]:dOut[12]:[{u'groupname':u'group1',u'members':[{u'hostname':u'localhost.localdomain',u'ip':u'112.65.140.132'},{u'hostname':u'test',u'ip':u'192.168.1.168'}]},{u'groupname':u'group2',u'members':[]}]In[13]:foriind:printi{u'groupname':u'group1',u'members':[{u'ip':u'112.65.140.132',u'hostname':u'localhost.localdomain'},{u'ip':u'192.168.1.168',u'hostname':u'test'}]}{u'groupname':u'group2',u'members':[]}

二、API-shell格式

方法相同

1、添加url:hostinfo/getshell/

[root@133simplecmdb-json]#vim/opt/python/django/simplecmdb-json/simplecmdb/urls.pyfromdjango.conf.urlsimportpatterns,include,urlfromdjango.contribimportadminadmin.autodiscover()urlpatterns=patterns('',#Examples:#url(r'^$','simplecmdb.views.home',name='home'),#url(r'^blog/',include('blog.urls')),url(r'^admin/',include(admin.site.urls)),url(r'^hostinfo/collect/$','hostinfo.views.collect'),url(r'^hostinfo/getjson/$','hostinfo.views.getjson'),url(r'^hostinfo/getshell/$','hostinfo.views.getshell'),)


2、在视图里面定义函数

vim/opt/python/django/simplecmdb-json/hostinfo/views.py#增加getshell的方法:defgetshell(req):res=''hg=HostGroup.objects.all()forginhg:groupname=g.groupnameforhing.members.all():hostname=h.hostnameip=h.ipres+=groupname+''+hostname+''+ip+'\n'returnHttpResponse(res)


3、访问测试:

访问测试[root@133simplecmdb-json]#curlhttp://112.65.140.133:8080/hostinfo/getshell/group1localhost.localdomain112.65.140.132group1test192.168.1.168In[1]:importurllib,urllib2In[2]:urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getshell/')Out[2]:<addinfourlat33701040whosefp=<socket._fileobjectobjectat0x1e74150>>In[3]:req=urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getshell/')In[4]:req.read()Out[4]:'group1localhost.localdomain112.65.140.132\ngroup1test192.168.1.168\n'