CMDB资产管理系统开发【day26】:CMDB上节回顾

一、上节知识点回顾

  1. 服务器设计了一个表结构
  2. 开发了一个客户端

二、后台创建缓存区表

 客户端连接服务器,在服务器的下面看报错信息

因为URL都没有写,所以我找不到呀

1、在MadKing\url.py 文件下加如下内容

 url(r'^asset/',include(asset_urls)),
	'''
	你看我把所有的以asset开头的请求都转到了asset_urls下,为什么要这样做?
	因为在项目开发的时候,有很多的项目是很多的组开发的,如果你把url写到全局
	的话,每次更新都会改所有的url,全局有可能影响很多项目,为了不影响全局,
	所以,每个app就单独写一个url
	'''

2、在asset\url.py文件加如下内容:

    url(r'report/asset_with_no_asset_id/$', views.asset_with_no_asset_id, name='acquire_asset_id'),
	
	'''
	这个name是干什么用的?在模板语言里,如果你想取一个url的话,
	你难道在模板里写report/asset_with_no_asset_id这个写死?
	是一个变量应该这样写{%url 'acquire_asset_id'%}
	'''

3、在asset\views.py文件加如下内容:

@csrf_exempt
def asset_with_no_asset_id(request):
    if request.method == 'POST':
        ass_handler = core.Asset(request)
        res = ass_handler.get_asset_id_by_sn()

        # return render(request,'assets/acquire_asset_id_test.html',{'response':res})
        return HttpResponse(json.dumps(res))
'''
客户端发过来的数据是post没错吧!
我没写试图里是不,因为客户端发送过来的数据是json格式
我把提取客户端数据的功能单独写了一个模块core.PY
'''

 

4、asset\core.py

 

class Asset(object):
    def __init__(self, request):
	'''
	初始化,只要一实例化肯定会走,我在试图里调用的时候为什么先把 request传进去了呢?
	因为客户端发过来的数据在request
	'''
        self.request = request
        self.mandatory_fields = ['sn', 'asset_id', 'asset_type']  # must contains 'sn' , 'asset_id' and 'asset_type'
			'''
			mandatory_fields是什么意思?意思是必须的必要的
			客户端回报数据我是不是要验证他的合法性,必须包裹这些字段
			如果没有我就认为你是不合法的,我把'sn', 'asset_id', 'asset_type'作为必须字段
			不能是from验证,为什么不能是from验证,因为我的数据提交过来的不是像网页那样的from表单数据
			'''
        self.field_sets = {
            'asset': ['manufactory'],
            'server': ['model', 'cpu_count', 'cpu_core_count', 'cpu_model', 'raid_type', 'os_type', 'os_distribution',
                       'os_release'],
            'networkdevice': []
        }
        self.response = {
            'error': [],
            'info': [],
            'warning': []
        }
	 '''self.response返回数据,给用户返回的信息分别放到不同的列表里'''



def get_asset_id_by_sn(self):
        '''When the client first time reports it's data to Server,it doesn't know 
		it's asset id yet,so it will come to the server asks for the asset it first,then report the data again 
		当第一次数据汇报过来的时候,不知道自己的ID拿到资产ID再来汇报,当他没有资产ID的时候先过来问一下
		'''



def asset_with_no_asset_id(request):
    if request.method == 'POST':
		print(request.POST.get("asset_data"))
		'''打印获取数据'''
        ass_handler = core.Asset(request)
        res = ass_handler.get_asset_id_by_sn()

        # return render(request,'assets/acquire_asset_id_test.html',{'response':res})
        return HttpResponse(json.dumps(res))

获取客户端发来的数据

windows客户端发送收集的数据报错问题:

默认是通过@csrf_exempt这个装饰器验证,这是验证浏览器发送过来的数据,但是我们的数据不是浏览器发送的
所以我们改成不验证这个,

在MadKing\settings.py下加上ALLOWED_HOSTS = ['192.168.10.22',]

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'p3l%y*#k*ung#9%s9srr65vwg1947s2e3h(2bcfi32jg8=f1u1'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['192.168.10.22',]

里的allowed_host加入IP地址就没有问题了,是因为配置的时候是0.0.0.0访问的是192.168.10.22.改成一致就可以

再次运行手机数据成功

 

posted @ 2018-06-24 18:34  活的潇洒80  阅读(312)  评论(0)    收藏  举报