上一篇博客聊到以下内容

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 pickled = {'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 picklewith 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 jsonIn [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 jsonIn [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 = hostnamehost.ip = iphost.osversion = osversionhost.memory = memoryhost.disk = diskhost.vendor_id = vendor_idhost.model_name = model_namehost.cpu_core = cpu_corehost.product = producthost.Manufacturer = Manufacturerhost.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-14from 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]breakreturn hostname#####[ipaddr message]#####
def get_Ipaddr():P = Popen(['ifconfig'],stdout=PIPE)data = P.stdout.read()list = []str = ''option = Falselines = data.split('\n')for line in lines:if not line.startswith(' '):list.append(str)str = lineelse:str += linewhile True:if '' in list:list.remove('')else:breakr_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/1000Memory_G2 = '%.2f' % Memory_Gmemory = Memory_G2 + 'G'return memory#####[disk message]#####
def get_DiskInfo():p = Popen(['fdisk','-l'],stdout=PIPE,stderr=PIPE)stdout,stderr = p.communicate()diskdata = stdoutdisk_initial_size = 0re_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_Greturn 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]) + 1res_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,snif __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 = hostnamehost.ip = iphost.osversion = osversionhost.memory = memoryhost.disk = diskhost.vendor_id = vendor_idhost.model_name = model_namehost.cpu_core = cpu_corehost.product = producthost.Manufacturer = Manufacturerhost.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-14from 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]breakreturn hostname#####[ipaddr message]#####
def get_Ipaddr():P = Popen(['ifconfig'],stdout=PIPE)data = P.stdout.read()list = []str = ''option = Falselines = data.split('\n')for line in lines:if not line.startswith(' '):list.append(str)str = lineelse:str += linewhile True:if '' in list:list.remove('')else:breakr_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/1000Memory_G2 = '%.2f' % Memory_Gmemory = Memory_G2 + 'G'return memory#####[disk message]#####
def get_DiskInfo():p = Popen(['fdisk','-l'],stdout=PIPE,stderr=PIPE)stdout,stderr = p.communicate()diskdata = stdoutdisk_initial_size = 0re_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_Greturn 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]) + 1res_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,snif __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)刷新浏览器,会看到再次新添加了一行

转载于:https://blog.51cto.com/467754239/1616806

Python [6] IT资产管理(下)相关推荐

  1. Python培训班线上线下哪种靠谱

    Python近几年在人工智能领域的快速发展,引起了很多人的注意,各种Python培训机构也越来越多,很多零基础的同学都想通过报培训班学习,目前互联网的发达,Python培训分为线上和线下,那么Pyth ...

  2. 使用Python批量删除windows下特定目录的N天前的旧文件实战:Windows下批量删除旧文件、清除缓存文件、解救C盘、拒绝C盘爆炸

    使用Python批量删除windows下特定目录的N天前的旧文件实战:Windows下批量删除旧文件.清除缓存文件.解救C盘.拒绝C盘爆炸 目录

  3. Python多分类问题下,micro-PR计算以及macro-PR计算

    Python多分类问题下,micro-PR计算以及macro-PR计算 #二分类的Precision和Recall计算公式 在普通的二分类问题中通过混淆矩阵可以轻松地计算出来Precison和Reca ...

  4. python中标识符下划线用作开头_python python中那些双下划线开头的那些函数都是干啥用用的...

    1.写在前面 今天遇到了__slots__,,所以我就想了解下python中那些双下划线开头的那些函数都是干啥用用的,翻到了下面这篇博客,看着很全面,我只了解其中的一部分,还不敢乱下定义. 其实如果足 ...

  5. python查找文件夹下的文件,python 查找文件夹下所有文件 实现代码 -电脑资料

    复制代码代码如下: def find_file_by_pattern(pattern='.*', base=".", circle=True): '''''查找给定文件夹下面所有 ...

  6. Python TimedRotatingFileHandler 多进程环境下的问题和解决方法

    Python TimedRotatingFileHandler 多进程环境下的问题和解决方法 原文:https://my.oschina.net/lionets/blog/796438 Python ...

  7. python打开一个文件夹下所有txt文件-python读取一个目录下所有txt里面的内容方法...

    实例如下所示: import os allFileNum = 0 def printPath(level, path): global allFileNum ''''' 打印一个目录下的所有文件夹和文 ...

  8. 如何打开python的交互窗口-Python多版本情况下四种快速进入交互式命令行的操作技巧...

    原标题:Python多版本情况下四种快速进入交互式命令行的操作技巧 因为工作需求或者学习需要等原因,部分小伙伴的电脑中同时安装了Python2和Python3,相信在Python多版本的切换中常常会遇 ...

  9. python怎么导入txt文件夹-python读取一个目录下所有txt里面的内容方法

    实例如下所示: import os allFileNum = 0 def printPath(level, path): global allFileNum ''''' 打印一个目录下的所有文件夹和文 ...

  10. Windows+Python 3.6环境下安装PyQt4

    Windows+Python 3.6环境下PyQt4安装不上 文章目录: 一.PyQt4安装不上 二.正确安装PyQt4库包 最近在写的程序需要用到UI界面显示,然后又闻PyQt4可以干这个事,然后走 ...

最新文章

  1. monty python dead parrot-BBC十大英剧神作出炉!
  2. centos6上虚拟主机的实现
  3. JVM 年轻代(Eden、From、To)、老年代讲解
  4. nyoj1047欧几里得
  5. 跟着迪哥学python 经管之家_跟着迪哥学Python数据分析与机器学习实战
  6. 将状态机模式实现为流处理器
  7. 1.2 - 列表练习题
  8. WordPress主题-Qinmei视频主题3.0版本
  9. JQuery jsonp使用小记
  10. Linux下实现Rsync目录同步备份
  11. ORCAD PSPICE 仿真学习
  12. 计算机cpu结构实物图片,cpu内部结构显微图/cpu内部结构放大图
  13. mysql redo,MySQL 8.0 redo log的深入解析
  14. 买马桶哪个牌子的最好?
  15. 倾斜摄影当中重叠度、传感器尺寸、焦距等参数问题梳理
  16. 机器学习中对数据集进行拆分及模型训练
  17. HTTP Status 404 - The requested resource (/Test/loginSerlet) is not available.
  18. Adobe XMP SDK项目应用(续1)
  19. 如何做好微生物组学代谢组学联合分析研究?
  20. AcWing 4418. 选元素

热门文章

  1. 用户身份链接方法——DeepLink
  2. 【ArcGIS|空间分析】图像纠正
  3. 如何用计算机寒假计划表,如何制定寒假学习计划表
  4. 实习踩坑之路:ElasticSearch搜索出来了不是自己的数据?Elastic会像MyBatisPlus一样会帮我们做判断null的操作么?分片精确度如何控制?
  5. 将数组A中的内容和数组B中的内容进行交换,计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,1到 100 的所有整数中出现多少次数字9。
  6. 进击的 Flink:网易云音乐实时数仓建设实践
  7. 2019年10月全国程序员工资统计,一半以上的职位5个月没招到人~
  8. 漫画:老板给员工画饼记
  9. 17年,寻找出路的一年
  10. pb 执行insert 后return是否会自动提交_一条MySQL更新语句是怎么执行的?