Python [6] IT资产管理(下)
上一篇博客聊到以下内容
1、安装django
2、部署工程和应用
3、修改、添加工程和应用配置文件并能成功url访问
4、Python脚本采集主机信息
5、通过post方式传送搜集的信息到服务器端
6、主机分组
如需更详细的了解,请参考http://467754239.blog.51cto.com/4878013/1616551
在这篇博客中,我们针对上篇博客中的重点部分做阐述,如何多钟方式实现第5步:
5、通过post方式传送搜集的信息到服务器端
一、Python序列化
1、序列化是什么
序列化:内存里面有一个数据结构,你希望将它保存下来,重用,或者发送给其他人。你会怎么做?嗯, 这取决于你想要怎么保存,怎么重用,发送给谁。很多游戏允许你在退出的时候保存进度,然后你再次启动的时候回到上次退出的地方。(实际上, 很多非游戏程序也会这么干。) 在这个情况下, 一个捕获了当前进度的数据结构需要在你退出的时候保存到磁盘上,接着在你重新启动的时候从磁盘上加载进来。这个数据只会被创建它的程序使用,不会发送到网 络上,也不会被其它程序读取。因此,互操作的问题被限制在保证新版本的程序能够读取以前版本的程序创建的数据。
2、实现序列化的方法
pickle(python语言编写)、cPickle(c语言编写)
JSON
Shelve
YAML
3、pickle和JSON的基本使用
pickle
(1)pickle将字典序列化存储到文本文件中
[root@localhost test]# pwd
/tmp/test
[root@localhost test]# ls
1_pickle_dump.py 2_pickle_load.py
[root@localhost test]# python 1_pickle_dump.py
[root@localhost test]# ls
1_pickle_dump.py 2_pickle_load.py dump.txt #存储到磁盘的文件中
[root@localhost test]# cat dump.txt
(dp0
S'age'
p1
S'25'
p2
sS'tel'
p3
S'132600*****'
p4
sS'name'
p5
S'ZhengYanSheng'
p6
s.
[root@localhost test]# cat 1_pickle_dump.py
#!/usr/bin/env python
#
import pickle
d = {'name':'ZhengYanSheng','age':'25','tel':'132600*****'}
with open('/tmp/test/dump.txt','w') as fd:
pickle.dump(d,fd)
(2)pickle加载文本文件中的内容并生成一个新的字典
[root@localhost test]# pwd
/tmp/test
[root@localhost test]# ls
1_pickle_dump.py 2_pickle_load.py dump.txt
[root@localhost test]# python 2_pickle_load.py
{'age': '25', 'tel': '132600*****', 'name': 'ZhengYanSheng'} #生成一个新的字典
[root@localhost test]# cat 2_pickle_load.py
#!/usr/bin/env python
#
import pickle
with open('/tmp/test/dump.txt','r') as fd:
d1 = pickle.load(fd)
print d1
json
这次我们直接在Ipython的交互式中进行操作json的使用
(1)json的导出
[root@localhost ~]# ipython
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import json
In [2]: d = {'a':'a','b':235,'c':('c1','c2'),'d':'True','e':'None'}
In [3]: d
Out[3]: {'a': 'a', 'b': 235, 'c': ('c1', 'c2'), 'd': 'True', 'e': 'None'}
In [4]: with open('/tmp/test/d.json','w') as fd:
...: json.dump(d,fd) #写入到文件中
...:
Do you really want to exit ([y]/n)?
[root@localhost ~]# cat /tmp/test/d.json #在shell模式下查看写入的文件内容
{"a": "a", "c": ["c1", "c2"], "b": 235, "e": "None", "d": "True"}
(2)json的载入
[root@localhost ~]# ipython
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import json
In [2]: with open('/tmp/test/d.json','r') as fd:
...: dd = json.load(fd)
In [3]: print dd
{u'a': u'a', u'c': [u'c1', u'c2'], u'b': 235, u'e': u'None', u'd': u'True'}
二、多种以POST方式传参的列子
1、pickle的方式
(1)修改views.py文件
[root@localhost Simplecmdb]# cat hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host
import pickle #导入pickle模块
# Create your views here.
def index(req):
if req.method == 'POST':
pick_obj = pickle.loads(req.body) #接受客户端的传参是一个字典
hostname = pick_obj['hostname'] #既然是一个字典,那么我们就一个变量变量的接收
ip = pick_obj['ip']
osversion = pick_obj['osversion']
memory = pick_obj['memory']
disk = pick_obj['disk']
vendor_id = pick_obj['vendor_id']
model_name = pick_obj['model_name']
cpu_core = pick_obj['cpu_core']
product = pick_obj['product']
Manufacturer = pick_obj['Manufacturer']
sn = pick_obj['sn']
try:
host = Host.objects.get(hostname=hostname)
except:
host = Host()
host.hostname = hostname
host.ip = ip
host.osversion = osversion
host.memory = memory
host.disk = disk
host.vendor_id = vendor_id
host.model_name = model_name
host.cpu_core = cpu_core
host.product = product
host.Manufacturer = Manufacturer
host.sn = sn
host.save()
return HttpResponse('ok')
else:
return HttpResponse('no data')
(2)修改搜集主机信息脚本(需要修改一个地方)
[root@localhost Simplecmdb]# vim post_hostinfo.py
#!/usr/bin/env python
#coding:utf8
#author:Allentuns
#time:2015-02-14
from subprocess import Popen,PIPE
import urllib,urllib2
import pickle
import json
import re
###[hostname message]#####
def get_HostnameInfo(file):
with open(file,'r') as fd:
data = fd.read().split('\n')
for line in data:
if line.startswith('HOSTNAME'):
hostname = line.split('=')[1]
break
return hostname
#####[ipaddr message]#####
def get_Ipaddr():
P = Popen(['ifconfig'],stdout=PIPE)
data = P.stdout.read()
list = []
str = ''
option = False
lines = data.split('\n')
for line in lines:
if not line.startswith(' '):
list.append(str)
str = line
else:
str += line
while True:
if '' in list:
list.remove('')
else:
break
r_devname = re.compile('(eth\d*|lo)')
r_mac = re.compile('HWaddr\s([A-F0-9:]{17})')
r_ip = re.compile('addr:([\d.]{7,15})')
for line in list:
devname = r_devname.findall(line)
mac = r_mac.findall(line)
ip = r_ip.findall(line)
if mac:
return ip[0]
#####[osversion message]#####
def get_OsVerion(file):
with open(file) as fd:
lines = fd.readlines()
os_version = lines[0][:-8]
return os_version
#####[memory message]#####
def get_MemoryInfo(file):
with open(file) as fd:
data_list = fd.read().split('\n')
MemTotal_line = data_list[0]
Memory_K = MemTotal_line.split()[1]
Memory_G = float(Memory_K)/1000/1000
Memory_G2 = '%.2f' % Memory_G
memory = Memory_G2 + 'G'
return memory
#####[disk message]#####
def get_DiskInfo():
p = Popen(['fdisk','-l'],stdout=PIPE,stderr=PIPE)
stdout,stderr = p.communicate()
diskdata = stdout
disk_initial_size = 0
re_disk_type = re.compile(r'Disk /dev/[shd]{1}.*:\s+[\d.\s\w]*,\s+([\d]+).*')
disk_size_bytes = re_disk_type.findall(diskdata)
for size in disk_size_bytes:
disk_initial_size += int(size)
disk_size_total_bytes = '%.2f' % (float(disk_initial_size)/1000/1000/1000)
disk_size_total_G = disk_size_total_bytes + 'G'
disk = disk_size_total_G
return disk
#####[cpu message]#####
def get_CpuInfo():
p = Popen(['cat','/proc/cpuinfo'],stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
cpudata = stdout.strip()
cpu_dict = {}
re_cpu_cores = re.compile(r'processor\s+:\s+([\d])')
re_vendor_id = re.compile(r'vendor_id\s+:\s([\w]+)')
re_model_name = re.compile(r'model name\s+:\s+(.*)')
res_cpu_cores = re_cpu_cores.findall(cpudata)
cpu_dict['Cpu_Cores'] = int(res_cpu_cores[-1]) + 1
res_vendor_id = re_vendor_id.findall(cpudata)
cpu_dict['Vendor_Id'] = res_vendor_id[-1]
res_model_name = re_model_name.findall(cpudata)
cpu_dict['Model_Name'] = res_model_name[-1]
return cpu_dict
#####[Demi message]#####
def get_dmidecode():
P = Popen(['dmidecode'],stdout=PIPE)
data = P.stdout.read()
lines = data.split('\n\n')
dmidecode_line = lines[2]
line = [i.strip() for i in dmidecode_line.split('\n') if i]
Manufacturer = line[2].split(': ')[-1]
product = line[3].split(': ')[-1]
sn = line[5].split(': ')[-1]
return Manufacturer,product,sn
if __name__ == '__main__':
#####[get data]#####
hostname = get_HostnameInfo('/etc/sysconfig/network')
ip = get_Ipaddr()
osversion = get_OsVerion('/etc/issue')
memory = get_MemoryInfo('/proc/meminfo')
disk = get_DiskInfo()
Vendor_Id = get_CpuInfo()['Vendor_Id']
Model_Name = get_CpuInfo()['Model_Name']
Cpu_Cores = get_CpuInfo()['Cpu_Cores']
Manufacturer,product,sn = get_dmidecode()
#####[get dict]#####
hostinfo = {
'hostname':hostname,
'ip':ip,
'osversion':osversion,
'memory':memory,
'disk':disk,
'vendor_id':Vendor_Id,
'model_name':Model_Name,
'cpu_core':Cpu_Cores,
'product':product,
'Manufacturer':Manufacturer,
'sn':sn,
}
print hostinfo
#data = urllib.urlencode(hostinfo) #注释掉原来
data = pickle.dumps(hostinfo) #添加一行
req = urllib2.urlopen('http://192.168.1.210:80/hostinfo',data)
(3)执行此脚本
[root@localhost Simplecmdb]# python post_hostinfo.py
{'product': 'VMware Virtual Platform', 'ip': '192.168.1.210', 'vendor_id': 'GenuineIntel', 'cpu_core': 1, 'disk': '17.18G', 'hostname': 'localhost.localdomain', 'sn': 'VMware-56 4d 41 69 ad a2 e6 3c-84 eb 81 81 e9 b4 4a 54', 'memory': '0.50G', 'osversion': 'CentOS release 6.4 ', 'model_name': 'Intel(R) Xeon(R) CPU E5-2407 0 @ 2.20GHz', 'Manufacturer': 'VMware, Inc.'}
(4)刷新浏览器,会看到新添加了一行
2、json方式(和上述方法基本相同)
(1)修改views.py文件
[root@localhost Simplecmdb]# cat hostinfo/views.py
from django.shortcuts import render
from django.http import HttpResponse
from hostinfo.models import Host
import pickle
import json #导入模块
# Create your views here.
def index(req):
if req.method == 'POST':
pick_obj = json.loads(req.body) #修改此处
hostname = pick_obj['hostname']
ip = pick_obj['ip']
osversion = pick_obj['osversion']
memory = pick_obj['memory']
disk = pick_obj['disk']
vendor_id = pick_obj['vendor_id']
model_name = pick_obj['model_name']
cpu_core = pick_obj['cpu_core']
product = pick_obj['product']
Manufacturer = pick_obj['Manufacturer']
sn = pick_obj['sn']
try:
host = Host.objects.get(hostname=hostname)
except:
host = Host()
host.hostname = hostname
host.ip = ip
host.osversion = osversion
host.memory = memory
host.disk = disk
host.vendor_id = vendor_id
host.model_name = model_name
host.cpu_core = cpu_core
host.product = product
host.Manufacturer = Manufacturer
host.sn = sn
host.save()
return HttpResponse('ok')
else:
return HttpResponse('no data')
(2)修改搜集主机信息脚本(需要修改一个地方)
[root@localhost Simplecmdb]# cat post_hostinfo.py
#!/usr/bin/env python
#coding:utf8
#author:Allentuns
#time:2015-02-14
from subprocess import Popen,PIPE
import urllib,urllib2
import pickle
import json
import re
###[hostname message]#####
def get_HostnameInfo(file):
with open(file,'r') as fd:
data = fd.read().split('\n')
for line in data:
if line.startswith('HOSTNAME'):
hostname = line.split('=')[1]
break
return hostname
#####[ipaddr message]#####
def get_Ipaddr():
P = Popen(['ifconfig'],stdout=PIPE)
data = P.stdout.read()
list = []
str = ''
option = False
lines = data.split('\n')
for line in lines:
if not line.startswith(' '):
list.append(str)
str = line
else:
str += line
while True:
if '' in list:
list.remove('')
else:
break
r_devname = re.compile('(eth\d*|lo)')
r_mac = re.compile('HWaddr\s([A-F0-9:]{17})')
r_ip = re.compile('addr:([\d.]{7,15})')
for line in list:
devname = r_devname.findall(line)
mac = r_mac.findall(line)
ip = r_ip.findall(line)
if mac:
return ip[0]
#####[osversion message]#####
def get_OsVerion(file):
with open(file) as fd:
lines = fd.readlines()
os_version = lines[0][:-8]
return os_version
#####[memory message]#####
def get_MemoryInfo(file):
with open(file) as fd:
data_list = fd.read().split('\n')
MemTotal_line = data_list[0]
Memory_K = MemTotal_line.split()[1]
Memory_G = float(Memory_K)/1000/1000
Memory_G2 = '%.2f' % Memory_G
memory = Memory_G2 + 'G'
return memory
#####[disk message]#####
def get_DiskInfo():
p = Popen(['fdisk','-l'],stdout=PIPE,stderr=PIPE)
stdout,stderr = p.communicate()
diskdata = stdout
disk_initial_size = 0
re_disk_type = re.compile(r'Disk /dev/[shd]{1}.*:\s+[\d.\s\w]*,\s+([\d]+).*')
disk_size_bytes = re_disk_type.findall(diskdata)
for size in disk_size_bytes:
disk_initial_size += int(size)
disk_size_total_bytes = '%.2f' % (float(disk_initial_size)/1000/1000/1000)
disk_size_total_G = disk_size_total_bytes + 'G'
disk = disk_size_total_G
return disk
#####[cpu message]#####
def get_CpuInfo():
p = Popen(['cat','/proc/cpuinfo'],stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
cpudata = stdout.strip()
cpu_dict = {}
re_cpu_cores = re.compile(r'processor\s+:\s+([\d])')
re_vendor_id = re.compile(r'vendor_id\s+:\s([\w]+)')
re_model_name = re.compile(r'model name\s+:\s+(.*)')
res_cpu_cores = re_cpu_cores.findall(cpudata)
cpu_dict['Cpu_Cores'] = int(res_cpu_cores[-1]) + 1
res_vendor_id = re_vendor_id.findall(cpudata)
cpu_dict['Vendor_Id'] = res_vendor_id[-1]
res_model_name = re_model_name.findall(cpudata)
cpu_dict['Model_Name'] = res_model_name[-1]
return cpu_dict
#####[Demi message]#####
def get_dmidecode():
P = Popen(['dmidecode'],stdout=PIPE)
data = P.stdout.read()
lines = data.split('\n\n')
dmidecode_line = lines[2]
line = [i.strip() for i in dmidecode_line.split('\n') if i]
Manufacturer = line[2].split(': ')[-1]
product = line[3].split(': ')[-1]
sn = line[5].split(': ')[-1]
return Manufacturer,product,sn
if __name__ == '__main__':
#####[get data]#####
hostname = get_HostnameInfo('/etc/sysconfig/network')
ip = get_Ipaddr()
osversion = get_OsVerion('/etc/issue')
memory = get_MemoryInfo('/proc/meminfo')
disk = get_DiskInfo()
Vendor_Id = get_CpuInfo()['Vendor_Id']
Model_Name = get_CpuInfo()['Model_Name']
Cpu_Cores = get_CpuInfo()['Cpu_Cores']
Manufacturer,product,sn = get_dmidecode()
#####[get dict]#####
hostinfo = {
'hostname':hostname,
'ip':ip,
'osversion':osversion,
'memory':memory,
'disk':disk,
'vendor_id':Vendor_Id,
'model_name':Model_Name,
'cpu_core':Cpu_Cores,
'product':product,
'Manufacturer':Manufacturer,
'sn':sn,
}
print hostinfo
#data = urllib.urlencode(hostinfo)
#data = pickle.dumps(hostinfo) #注释掉
data = json.dumps(hostinfo) #添加一行
req = urllib2.urlopen('http://192.168.1.210:80/hostinfo',data)
(3)执行此脚本
[root@localhost Simplecmdb]# python post_hostinfo.py
{'product': 'VMware Virtual Platform', 'ip': '192.168.1.210', 'vendor_id': 'GenuineIntel', 'cpu_core': 1, 'disk': '17.18G', 'hostname': 'localhost.localdomain', 'sn': 'VMware-56 4d 41 69 ad a2 e6 3c-84 eb 81 81 e9 b4 4a 54', 'memory': '0.50G', 'osversion': 'CentOS release 6.4 ', 'model_name': 'Intel(R) Xeon(R) CPU E5-2407 0 @ 2.20GHz', 'Manufacturer': 'VMware, Inc.'}
(4)刷新浏览器,会看到再次新添加了一行
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341