使用autoserver的代码测试
在api的project的url中配置路由
/home/ningherui/PycharmProjects/cmdb_first_step/autoserver/api/urls.py
from django.contrib import admin from django.urls import path from django.conf.urls import url,include from api import views urlpatterns = [ url('get_data', views.get_data), url('test', views.test) ]
配置view
/home/ningherui/PycharmProjects/cmdb_first_step/autoserver/api/views.py
import json from django.shortcuts import render,HttpResponse from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt # Create your views here. @csrf_exempt def get_data(request): #print(request.body) #序列化和反序列化 content = request.body.decode('utf-8') server_info_dict = json.loads(content) print(server_info_dict) #获取数据之后,把他们放到数据库,然后使用web的APP展示数据 return HttpResponse('成功') def test(request): result = {'status':True,'data':None,'error':None} nid = request.GET.get('id') if nid == 10: result['data'] = '数据正确,已收到' else: result['error'] = '错误' result['status'] = False return JsonResponse(result)
访问http://127.0.0.1:8000/api/test
封装一个对象(领域驱动模型)
import json from django.shortcuts import render,HttpResponse from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt # Create your views here. @csrf_exempt def get_data(request): #print(request.body) #序列化和反序列化 content = request.body.decode('utf-8') server_info_dict = json.loads(content) print(server_info_dict) #获取数据之后,把他们放到数据库,然后使用web的APP展示数据 return HttpResponse('成功') class BaseResponse(object): def __init__(self,status=True,data=None,error=None): self.status = status self.data = data self.error = error @property def dict(self): return self.__dict__ def test(request): result = BaseResponse() nid = request.GET.get('id') if nid == 10: result.data = '数据正确,已收到.' else: result.error = '错误' result.status = False #返回json格式数据 return JsonResponse(result.dict) #如果没有dict的函数,可以直接返回return JsonResponse(result.__dict__) #return JsonResponse(result.__dict__)
结合日志和封装实现插件的数据封装
分别把硬盘,网卡,内存,主板,系统等信息使用ssh命令读取,然后使用parse方法进行数据处理,在进行封装
目录如下
代码上传到git
https://gitlab.com/ningherui/autoclient.git
感谢老男孩教育