Django通過(guò)json格式收集主機(jī)信息
更新時(shí)間:2020年05月29日 10:37:18 作者:Nyan
這篇文章主要介紹了基于Django收集主機(jī)信息json格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
代碼如下
Control:
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/gettxt/$','hostinfo.views.gettxt'), )
View:
from django.shortcuts import render from django.http import HttpResponse from hostinfo.models import Host,HostGroup import pickle import json # Create your views here. def collect(req): if req.POST: #dic = pickle.loads(req.body) dic = json.loads(req.body) hostname = dic['hostname'] ip = dic['ip'] product = dic['product'] sn = dic['sn'] vendor = dic['vendor'] cpu_model = dic['cpu_model'] cpu_num = dic['cpu_num'] memory = dic['memory'] osver = dic['osver'] try: host = Host.objects.get(sn=sn) except: host = Host() host.hostname = hostname host.ip = ip host.product = product host.sn = sn host.vendor = vendor host.cpu_model = cpu_model host.cpu_num = cpu_num host.memory = memory host.osver = osver host.save() return HttpResponse('data have save into DB') else: return HttpResponse('there is no data from POST method') 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)) def gettxt(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)
Model:
from django.db import models # Create your models here. class Host(models.Model): hostname = models.CharField(max_length=50) ip = models.IPAddressField() vendor = models.CharField(max_length=50) product = models.CharField(max_length=50) sn = models.CharField(max_length=50) cpu_model = models.CharField(max_length=50) cpu_num = models.IntegerField() memory = models.CharField(max_length=50) osver = models.CharField(max_length=50) def __unicode__(self): return self.hostname class HostGroup(models.Model): groupname = models.CharField(max_length=50) members = models.ManyToManyField(Host)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Django 實(shí)現(xiàn)將圖片轉(zhuǎn)為Base64,然后使用json傳輸
- Django 返回json數(shù)據(jù)的實(shí)現(xiàn)示例
- Django接收post前端返回的json格式數(shù)據(jù)代碼實(shí)現(xiàn)
- Django后端接收嵌套Json數(shù)據(jù)及解析詳解
- Django objects的查詢(xún)結(jié)果轉(zhuǎn)化為json的三種方式的方法
- Django分頁(yè)查詢(xún)并返回jsons數(shù)據(jù)(中文亂碼解決方法)
- 使用Django和Python創(chuàng)建Json response的方法
- 淺談django model postgres的json字段編碼問(wèn)題
相關(guān)文章
Python3.9.1中使用split()的處理方法(推薦)
這篇文章主要介紹了Python3.9.1中使用split()的處理方法(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02使用python查找替換PowerPoint演示文稿中的文本
演示文稿已成為商務(wù)會(huì)議、學(xué)術(shù)報(bào)告和教育培訓(xùn)中不可或缺的一部分,而PowerPoint演示文稿作為行業(yè)標(biāo)準(zhǔn)工具,更是承載著無(wú)數(shù)創(chuàng)意與信息的載體,本文將介紹如何使用Python來(lái)精確查找并替換PowerPoint演示文稿中的文本,需要的朋友可以參考下2024-07-07Python實(shí)現(xiàn)查找匹配項(xiàng)作處理后再替換回去的方法
這篇文章主要介紹了Python實(shí)現(xiàn)查找匹配項(xiàng)作處理后再替換回去的方法,涉及Python字符串查找、轉(zhuǎn)換、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Python高級(jí)property屬性用法實(shí)例分析
這篇文章主要介紹了Python高級(jí)property屬性用法,結(jié)合實(shí)例形式分析了Python property屬性的功能及各種常見(jiàn)的使用技巧,需要的朋友可以參考下2019-11-11Python子進(jìn)程subpocess原理及用法解析
這篇文章主要介紹了Python子進(jìn)程subpocess原理及用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07在Python中使用判斷語(yǔ)句和循環(huán)的教程
這篇文章主要介紹了在Python中使用判斷語(yǔ)句和循環(huán)的教程,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),代碼基于Python2.x,需要的朋友可以參考下2015-04-04python使用Random隨機(jī)生成列表的方法實(shí)例
在日常的生活工作和系統(tǒng)游戲等設(shè)計(jì)和制作時(shí),經(jīng)常會(huì)碰到產(chǎn)生隨機(jī)數(shù),用來(lái)解決問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python使用Random隨機(jī)生成列表的相關(guān)資料,需要的朋友可以參考下2022-04-04