一、API-json格式 (應(yīng)用程序接口)
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的雞西梨樹(shù)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!1、添加url:hostinfo/getjson/
[root@133 simplecmdb-json]# vim /opt/python/django/simplecmdb-json/simplecmdb/urls.py from django.conf.urls import patterns, include, url from django.contrib import admin admin.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、在視圖里面定義函數(shù)
[root@133 simplecmdb-json]# vim /opt/python/django/simplecmdb-json/hostinfo/views.py from hostinfo.models import Host,HostGroup #需要倒入HostGroup #增加一個(gè)函數(shù), def getjson(req): ret_list = [] hg = HostGroup.objects.all() for g in hg: ret = {'groupname':g.groupname,'members':[]} for h in g.members.all(): ret_h = {'hostname':h.hostname,'ip':h.ip} ret['members'].append(ret_h) ret_list.append(ret) return HttpResponse(json.dumps(ret_list))
3、web訪問(wèn):http://112.65.140.13:8080/hostinfo/getjson/得到Json數(shù)據(jù):
[root@133 simplecmdb-json]# curl http://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]: import urllib,urllib2 In [6]: urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getjson/') Out[6]:> 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]: import json In [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]: d Out[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]: for i in d : print i {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@133 simplecmdb-json]# vim /opt/python/django/simplecmdb-json/simplecmdb/urls.py from django.conf.urls import patterns, include, url from django.contrib import admin admin.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、在視圖里面定義函數(shù)
vim /opt/python/django/simplecmdb-json/hostinfo/views.py #增加getshell的方法: def getshell(req): res = '' hg = HostGroup.objects.all() for g in hg: groupname = g.groupname for h in g.members.all(): hostname = h.hostname ip = h.ip res += groupname+' '+hostname+' '+ip +'\n' return HttpResponse(res)
3、訪問(wèn)測(cè)試:
訪問(wèn)測(cè)試 [root@133 simplecmdb-json]# curl http://112.65.140.133:8080/hostinfo/getshell/ group1 localhost.localdomain 112.65.140.132 group1 test 192.168.1.168 In [1]: import urllib,urllib2 In [2]: urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getshell/') Out[2]:> In [3]: req = urllib2.urlopen('http://112.65.140.133:8080/hostinfo/getshell/') In [4]: req.read() Out[4]: 'group1 localhost.localdomain 112.65.140.132\ngroup1 test 192.168.1.168\n'
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。