API-json格式、API-shell格式
2017-03-30 17:32
411 查看
一、API-json格式 (应用程序接口)
1、添加url:hostinfo/getjson/
2、在视图里面定义函数
方法相同
1、添加url:hostinfo/getshell/
2、在视图里面定义函数
3、访问测试:
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、在视图里面定义函数
[root@133 simplecmdb-json]# vim /opt/python/django/simplecmdb-json/hostinfo/views.py from hostinfo.models import Host,HostGroup #需要倒入HostGroup #增加一个函数, 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访问:http://112.65.140.13:8080/hostinfo/getjson/得到Json数据:
[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]: <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 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、在视图里面定义函数
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、访问测试:
访问测试 [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]: <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'
相关文章推荐
- Python[7] :Python制作json格式和shell格式的API
- 中国天气网api(json格式)
- 中国天气网api(json格式)
- 备忘笔记:关于写一个输出为Jackson格式(如xml、json)的api的小笔记
- JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
- 当自定义数据属性为json格式字符串时jQuery的data api问题探讨
- Unity3D通过PHP输出的JSON格式API
- 【Web】前台传送JSON格式数据到后台Shell处理
- 利用Shell脚本将MySQL表中的数据转化为json格式
- java访问zabbix api中文提示json格式错误
- 中国天气网api(json格式)
- 中国天气网api(json格式)
- 百度天气api开发 全解析xml和json格式 使用json-lib jsonObject、 Gson 方式把json 转换成 java对象
- 不同Json api生成json数据格式的区别
- 写一个json格式API,http请求接收json数据
- wordprss 的数据导出的api为json格式 问题
- Github API 库的JSON文件格式
- JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口
- 调用splunk api时获得json格式的数据