91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用代碼詳解Django如何通過json格式收集主機信息

發布時間:2020-07-20 17:34:20 來源:億速云 閱讀:135 作者:小豬 欄目:開發技術

小編這次要給大家分享的是用代碼詳解Django如何通過json格式收集主機信息,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

代碼如下

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)

看完這篇關于用代碼詳解Django如何通過json格式收集主機信息的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

远安县| 美姑县| 酒泉市| 霍州市| 南充市| 凤城市| 梅河口市| 浏阳市| 衡东县| 郓城县| 汝城县| 蓬莱市| 萍乡市| 平谷区| 泽库县| 城口县| 永修县| 雷山县| 连城县| 鹤庆县| 伊春市| 和平县| 逊克县| 新泰市| 勐海县| 木里| 公主岭市| 托克逊县| 吴江市| 郓城县| 岳池县| 达日县| 乌恰县| 台中县| 九龙城区| 彭阳县| 景泰县| 松桃| 勐海县| 新邵县| 界首市|