Django SimpleCMDB API
编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getjson 时,返回 json 格式的主机组和组成员信息:
[root@localhost SimpleCMDB]$ cat SimpleCMDB/urls.py
....
urlpatterns = patterns('',
....
url(r'^hostinfo/getjson/$', 'hostinfo.views.getjson'),
)
[root@localhost SimpleCMDB]$ cat hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host, HostGroup
import json .... def getjson(request):
data = []
host_group = HostGroup.objects.all()
for group in host_group:
result = {'groupname': group.group_name, 'members': []}
for hosts in group.group_members.all():
hostinfo = {'hostname': hosts.hostname,
'ip': hosts.ip,
'vendor': hosts.vendor,
'product': hosts.product,
'sn': hosts.sn,
'cpu_model': hosts.cpu_model,
'cpu_num': hosts.cpu_num,
'memory': hosts.memory,
'osver': hosts.osver}
result['members'].append(hostinfo)
data.append(result) return HttpResponse(json.dumps(data))

编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getshell 时,返回 shell 格式的主机组和组成员信息(返回 shell 格式的信息,主要是为了我们能在 Linux 下使用 grep 、awk 等对返回的数据做进一步处理):
[root@localhost SimpleCMDB]$ cat SimpleCMDB/urls.py
....
urlpatterns = patterns('',
....
url(r'^hostinfo/getshell/$', 'hostinfo.views.getshell'),
)
[root@localhost SimpleCMDB]$ cat hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host, HostGroup
import json ....def getshell(request):
data = ''
host_group = HostGroup.objects.all()
for group in host_group:
groupname = group.group_name
for hosts in group.group_members.all():
hostname = hosts.hostname
ip = hosts.ip
vendor = hosts.vendor
product = hosts.product
sn = hosts.sn
cpu_model = hosts.cpu_model
cpu_num = hosts.cpu_num
memory = hosts.memory
osver = hosts.osver
data += groupname + ' ' + hostname + ' ' + ip + ' ' + osver + ' ' + memory + '\n' return HttpResponse(data)

Django SimpleCMDB API的更多相关文章
- 初识Django —Python API接口编程入门
初识Django —Python API接口编程入门 一.WEB架构的简单介绍 Django是什么? Django是一个开放源代码的Web应用框架,由Python写成.我们的目标是用Python语言, ...
- Django QuerySet API文档
在查询时发生了什么(When QuerySets are evaluated) QuerySet 可以被构造,过滤,切片,做为参数传递,这些行为都不会对数据库进行操作.只要你查询的时候才真正的操作数据 ...
- tastypie Django REST API developement 1)
Read by linux/GNU commands Let's follow and start from here:http://django-tastypie.readthedocs.org/e ...
- $ Django 调API的几种方式
API调用方式 下面是python中会用到的库.urllib2httplib2pycurlrequestsurllib2 #request import requests, json github_u ...
- day 68 django 之api操作 | jQueryset集合与对象
我们的orm里面分为: jQueryset集合, 还有对象, 我们的jqueryset集合里面可以有多个对象,这句话的意思就是我们的对象是最小的单位,不可以再拆分了,我们的jQueryset集合就相当 ...
- python基于django编写api+前端后端分离
有用 https://segmentfault.com/a/1190000016049962#articleHeader2 python的前后端分离(一):django+原生js实现get请求 htt ...
- Django SimpleCMDB 项目
创建 SimpleCMDB 项目: [root@localhost ~]$ django-admin.py startproject SimpleCMDB 创建应用,收集主机信息: [root@loc ...
- Django SimpleCMDB 使用序列化
如下,前面我们是使用 urllib 方法来转换并传递数据的: [root@localhost ~]$ tail /data/script/getHostInfo.py if __name__ == ' ...
- Django SimpleCMDB WSGI
一.WSGI 介绍 (1) 在前面的学习中,我们是通过 python manage.py runserver 0.0.0.0:8000 来启动并访问开发服务器的:(2) 但在实际中我们是通过直接访问 ...
随机推荐
- add new row to data.frame/dataframe
df<-NULL new_row<-data.frame(colA="xxx",colB=123) df<-rbind(df,new_row)
- USB2.0学习笔记连载(十九):EZ-USB TRM手册重要部分介绍
TRM手册中给出了所有的寄存器配置,在 slave fifo模式或者 GPIF模式等,所以对于用到的各种寄存器配置需要查看此手册,当然还可以配合着应用手册<AN61345>. ...
- struts2客户端与服务器端即jsp页面与action之间的关系
在Struts2中,客户端和服务器之间的数据传输全部要用到get.set方法:用set方法 ,可以将表单中的值存入Action类.通过Struts2.0标签,调用get方法将Action类中的结果数据 ...
- Assets/FollowDestination.cs(6,13): error CS0246: The type or namespace name `NavMeshAgent' could not be found. Are you missing `UnityEngine.AI' using directive?的解决方案
问题的出现与描述 在Unity中创建一个NPC,使它一直跟踪一个目标Destination,C#脚本代码如下,错误信息描述如下 using System.Collections; using Syst ...
- C++ 把枚举变量的名称,直接当字符串使用方法 字符串化符号 #
#include <stdio.h> #include <typeinfo> #include <string> using namespace std; enum ...
- Servlet下载文件迅雷不支持问题真相之一
问题描述 最近在做一个下载文件的Servlet,直接使用浏览器的下载功能,完美支持,结果测试人员使用迅雷下载,就不行了,下载也能成功完成,只是迅雷下载的文件大小是悲催的0KB 真相搜罗 网上有很多帖子 ...
- asp.net mvc中配置路由默认值(Area中)
public class RouteConfig { ] { "Best.Site.Areas.BestPalace" }; public static void Register ...
- IMP导入时的错误以及解决办法
导出命令:exp 用户名/密码@localhost:5050/bkcyunty file=D:\bak\db.dmp log=D:\bak\db.log INDEXES=n STATISTICS=no ...
- eclipse .setting下各文件详解
Eclipse项目中系统文件介绍 一. 写在前面 文章较长,可以直接到感兴趣的段落,或者直接关键字搜索: 请原谅作者掌握的编程语言少,这里只研究Java相关的项目: 每一个文件仅仅做一个常见内容的简单 ...
- QQ彩票任意订阅内容导致骚扰用户
数据包: POST /my/index.php?mod=securityinfo&op=AjaxUpdateTipsSet HTTP/1.1 Host: 888.sports.qq.com C ...