Api-json format

Source: Internet
Author: User

One, Api-json format (application interface)


1. Add url:hostinfo/getjson/

[Email protected] simplecmdb-json]# Vim/opt/python/django/simplecmdb-json/simplecmdb/urls.pyfrom django.conf.urls     Import patterns, include, Urlfrom django.contrib import adminadmin.autodiscover () Urlpatterns = Patterns ("', # Examples: # URL (R ' ^$ ', ' simplecmdb.views.home ', name= ' home '), # URL (r ' ^blog/', include (' Blog.urls ')), url (r ' ^admin/', incl Ude (admin.site.urls)), url (r ' ^hostinfo/collect/$ ', ' hostinfo.views.collect '), url (r ' ^hostinfo/getjson/$ ', ' Hostinfo.views.getjson '),)


2. Define functions in the view

[[email protected] simplecmdb-json]# vim /opt/python /django/simplecmdb-json/hostinfo/views.pyfrom hostinfo.models import host,hostgroup # Need to pour hostgroup# add a function, 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 Access: http://112.65.140.13:8080/hostinfo/getjson/ get JSON data:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/66/wKiom1jc0THCMELfAAC9bBoEFyc683.png-wh_500x0-wm_ 3-wmp_4-s_3485611479.png "title=" 40c96f40-011d-4cef-a148-5e22c5011a3b.png "alt=" Wkiom1jc0thcmelfaac9bboefyc683.png-wh_50 "/>

[[email protected] 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,urllib2in [6]:  Urllib2.urlopen (' http://112.65.140.133:8080/hostinfo/getjson/') out[6]: <addinfourl at  33716920 whose fp = <socket._fileobject object at 0x1fc33d0>>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 jsonin [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]: 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 ':  []} 

Second, Api-shell format

Same method

1. Add url:hostinfo/getshell/

[Email protected] simplecmdb-json]# Vim/opt/python/django/simplecmdb-json/simplecmdb/urls.pyfrom django.conf.urls     Import patterns, include, Urlfrom django.contrib import adminadmin.autodiscover () Urlpatterns = Patterns ("', # Examples: # URL (R ' ^$ ', ' simplecmdb.views.home ', name= ' home '), # URL (r ' ^blog/', include (' Blog.urls ')), url (r ' ^admin/', incl Ude (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. Define functions in the view

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.host Name IP = H.ip Res + = groupname+ ' +hostname+ ' +ip + ' \ n ' return HttpResponse (RES)


3. Access test:

Access test [[email protected] simplecmdb-json]# Curl Http://112.65.140.133:8080/hostinfo/getshell/group1 Localhost.localdomain 112.65.140.132group1 test 192.168.1.168In [1]: Import urllib,urllib2in [2]: Urllib2.urlopen (' Http://112.65.140.133:8080/hostinfo/getshell/') out[2]: <addinfourl at 33701040 whose fp = <socket._fileobject Object at 0x1e74150>>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 '



This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1911838

Api-json format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.