日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python-Django收集主机信息

發(fā)布時(shí)間:2025/5/22 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python-Django收集主机信息 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.創(chuàng)建工程simplecmdb

django-admin.py startproject simplecmdb

2.創(chuàng)建應(yīng)用

cd simplecmdb

python manage.py startapp hostinfo

3.編輯配置文件

vim simplecmdb/setting.py

1 INSTALLED_APPS = ( 2 'django.contrib.admin', 3 'django.contrib.auth', 4 'django.contrib.contenttypes', 5 'django.contrib.sessions', 6 'django.contrib.messages', 7 'django.contrib.staticfiles', 8 'hostinfo' 9 #添加應(yīng)用 10 ) 11 12 MIDDLEWARE_CLASSES = ( 13 'django.contrib.sessions.middleware.SessionMiddleware', 14 'django.middleware.common.CommonMiddleware', 15 # 'django.middleware.csrf.CsrfViewMiddleware', 16 #注釋csrf方便使用第三方工具curl 17 'django.contrib.auth.middleware.AuthenticationMiddleware', 18 'django.contrib.messages.middleware.MessageMiddleware', 19 'django.middleware.clickjacking.XFrameOptionsMiddleware', 20 )

4.添加model

1 from django.db import models 2 3 # Create your models here. 4 5 class Host(models.Model): 6 hostname = models.CharField(max_length=50) 7 ip = models.IPAddressField() 8 vendor = models.CharField(max_length=50) 9 product = models.CharField(max_length=50) 10 sn = models.CharField(max_length=50) 11 cpu_model = models.CharField(max_length=50) 12 cpu_num = models.IntegerField() 13 memory = models.CharField(max_length=50) 14 osver = models.CharField(max_length=50)

5.初始化數(shù)據(jù)庫(kù)

python manage.py sqlall hostinfo

python manage,py syncdb

6.編輯url.py,添加url

url(r'^hostinfo/collect/','hostinfo.views.collect')

7.編輯views.py

1 from django.shortcuts import render 2 from django.http import HttpResponse 3 from hostinfo.models import Host 4 5 # Create your views here. 6 7 def collect(req): 8 if req.POST: 9 hostname = req.POST.get('hostname') 10 ip = req.POST.get('ip') 11 product = req.POST.get('product') 12 sn = req.POST.get('sn') 13 vendor = req.POST.get('vendor') 14 cpu_model = req.POST.get('cpu_model') 15 cpu_num = req.POST.get('cpu_num') 16 memory = req.POST.get('memory') 17 osver = req.POST.get('osver') 18 19 host = Host() 20 host.hostname = hostname 21 host.ip = ip 22 host.product = product 23 host.sn = sn 24 host.vendor = vendor 25 host.cpu_model = cpu_model 26 host.cpu_num = cpu_num 27 host.memory = memory 28 host.osver = osver 29 30 host.save() 31 32 return HttpResponse('data have save into DB') 33 else: 34 return HttpResponse('there is no data from POST method')

8.注冊(cè)model到admin界面

1 from django.contrib import admin 2 from hostinfo.models import Host 3 4 # Register your models here. 5 6 class HostAdmin(admin.ModelAdmin): 7 list_display = [ 8 'hostname', 9 'ip', 10 'product', 11 'vendor', 12 'sn', 13 'cpu_model', 14 'cpu_num', 15 'memory', 16 'osver'] 17 18 admin.site.register(Host,HostAdmin)

9.啟動(dòng)服務(wù)

python manage.py runserver 0.0.0.0:8000

10.訪問

curl -d hostname='node02' -d ip='192.168.1.2' -d product='BL 380' -d sn='XX213' -d vendor='HP' -d cpu_model='Intel' -d cpu_num=1 -d memory='250G' -d osver='md 05c' http://192.168.1.120:8000/hostinfo/collect/

1 #!/usr/bin/env python 2 3 import urllib,urllib2 4 from subprocess import Popen,PIPE 5 6 def getIfconfig(): 7 p = Popen(['ifconfig'],stdout=PIPE) 8 data = p.stdout.read() 9 return data 10 11 def getDmi(): 12 p = Popen(['dmidecode'],stdout=PIPE) 13 data = p.stdout.read() 14 return data 15 16 def parseData(data): 17 parsed_data = [] 18 new_line = '' 19 data = [i for i in data.split('\n') if i] 20 for line in data: 21 if line[0].strip(): 22 parsed_data.append(new_line) 23 new_line = line +'\n' 24 else: 25 new_line += line+'\n' 26 parsed_data.append(new_line) 27 return [i for i in parsed_data if i] 28 29 def parseIfconfig(parsed_data): 30 parsed_data = [i for i in parsed_data if not i.startswith('lo')] 31 for lines in parsed_data: 32 line_list = lines.split('\n') 33 devname = line_list[0].split()[0] 34 macaddr = line_list[0].split()[-1] 35 ipaddr = line_list[1].split()[1].split(':')[1] 36 break 37 dic['ip'] =ipaddr 38 return dic 39 40 def parseDmi(parsed_data): 41 dic = {} 42 parsed_data = [i for i in parsed_data if i.startswith('System Information')] 43 parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i] 44 dmi_dic = dict([i.strip().split(':') for i in parsed_data]) 45 dic['vendor'] = dmi_dic['Manufacturer'].strip() 46 dic['product'] = dmi_dic['Product Name'].strip() 47 dic['sn'] = dmi_dic['Serial Number'].strip()[:15] 48 return dic 49 50 def getHostname(f): 51 with open(f) as fd: 52 for line in fd: 53 if line.startswith('HOSTNAME'): 54 hostname = line.split('=')[1].strip() 55 break 56 return {'hostname':hostname} 57 58 def getOsver(f): 59 with open(f) as fd: 60 for line in fd: 61 osver = line.strip() 62 break 63 return {'osver':osver} 64 65 def getCpu(f): 66 num = 0 67 with open(f) as fd: 68 for line in fd: 69 if line.startswith('processor'): 70 num +=1 71 if line.startswith('model name'): 72 cpu_model = line.split(':')[1].split() 73 cpu_model = cpu_model[0]+' '+cpu_model[-1] 74 return {'cpu_num':num,'cpu_model':cpu_model} 75 76 def getMemory(f): 77 with open(f) as fd: 78 for line in fd: 79 if line.startswith('MemTotal'): 80 mem = int(line.split()[1].strip()) 81 break 82 mem = "%d" % int(mem/1024.0)+'M' 83 return {'memory':mem} 84 85 86 if __name__ == '__main__': 87 dic = {} 88 data_ip = getIfconfig() 89 parsed_data_ip = parseData(data_ip) 90 ip = parseIfconfig(parsed_data_ip) 91 data_dmi = getDmi() 92 parsed_data_dmi = parseData(data_dmi) 93 dmi = parseDmi(parsed_data_dmi) 94 hostname = getHostname('/etc/sysconfig/network') 95 osver = getOsver('/etc/issue') 96 cpu = getCpu('/proc/cpuinfo') 97 mem = getMemory('/proc/meminfo') 98 dic.update(ip) 99 dic.update(dmi) 100 dic.update(hostname) 101 dic.update(osver) 102 dic.update(cpu) 103 dic.update(mem) 104 data = urllib.urlencode(dic) 105 req = urllib2.urlopen('http://192.168.1.120:8000/hostinfo/collect/',data) 106 print req.read()

?

轉(zhuǎn)載于:https://www.cnblogs.com/Nyan-Workflow-FC/p/5699347.html

總結(jié)

以上是生活随笔為你收集整理的python-Django收集主机信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲国产视频一区二区 | 91新网站 | 美女主播在线观看 | 九九九九九九精品 | 欧美在线免费 | av久色 | 一区二区三区四区视频在线观看 | 亚洲美女影院 | 精品一区二区三区免费毛片 | av调教 | 精品69| 超碰女优| 91啪国产在线 | 丁香花高清在线观看完整动漫 | 怡红院一区二区三区 | 图书馆的女友动漫在线观看 | 青青青在线视频免费观看 | 日本在线视频不卡 | 99re在线 | 人人九九| 免费 成 人 黄 色 | 日本一区二区免费视频 | 午夜宅男网 | 精品一区二区三区蜜臀 | 成人av激情 | 欧美人妻精品一区二区 | 不卡二区| 国产精品老熟女视频一区二区 | 麻豆精品国产精华精华液好用吗 | 美女破处视频 | 日本黄色高清 | 丰满少妇被猛烈进入一区二区 | 九九九九精品九九九九 | 少妇人妻一区二区 | 中国av在线 | 亚洲最新av在线 | 欧美二区在线 | 美女91网站 | 亚洲一区二区三区播放 | 免费黄色在线视频 | 巨胸大乳www视频免费观看 | 久久久久99精品成人片试看 | 777久久久精品一区二区三区 | 欧美另类69 | 少妇高潮21p | 美女国产精品 | av一区二区在线观看 | 超碰女| 午夜寂寞少妇 | 狠狠婷婷| 国产成人精品久久二区二区91 | 黑人巨大精品欧美一区二区蜜桃 | 69xxx国产| 国产精品福利视频 | 日本少妇毛茸茸高潮 | 日韩精品成人在线观看 | 日本黄色免费网站 | 蜜桃传媒 | 91成人观看| 国产一区二区毛片 | 波多野结衣视频免费在线观看 | 欧美亚洲国产成人 | 亚洲免费一级片 | wwwxxx日韩| av中出| 污污污污污污www网站免费 | 少妇激情一区二区三区视频 | 97超碰在线资源 | 成都4电影免费高清 | 91在线精品李宗瑞 | 亚洲天堂av在线免费观看 | 午夜影院网站 | 亚洲成人一二三区 | 日韩精品1区 | 麻豆啪啪 | 精品一区二区三区入口 | 天堂网在线观看视频 | 熊出没之冬日乐翻天免费高清观看 | 欧洲高潮三级做爰 | 久久亚洲AV成人无码一二三 | 操人网| 91手机视频 | 麻豆啪啪 | 91精品国产综合久久久久 | 白峰美羽在线播放 | 91最新地址| 三年大全国语中文版免费播放 | 88国产精品 | 欧美精品一区二区三 | 日日操夜夜草 | 国产玖玖视频 | 尤物视频在线观看国产性感 | 日本最黄网站 | 精品电影在线观看 | 婷婷综合六月 | 韩国成年人网站 | 亚洲三级欧美 | www.亚洲成人 | 欧美国产高清 |