Django项目:CMDB(服务器硬件资产自动采集系统)--02--02CMDB将服务器基本信息提交到API接口


AutoCmdb


# urls.py
"""AutoCmdb URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path # ————————02CMDB将服务器基本信息提交到API接口————————
from django.conf.urls import url
from django.conf.urls import include
# ————————02CMDB将服务器基本信息提交到API接口———————— urlpatterns = [
path('admin/', admin.site.urls), # ————————02CMDB将服务器基本信息提交到API接口————————
url(r'^api/', include('api.urls')),
# ————————02CMDB将服务器基本信息提交到API接口———————— ]
# urls.py

# urls.py
# ————————02CMDB将服务器基本信息提交到API接口————————
from django.conf.urls import url
from api import views urlpatterns = [
url(r'^asset$', views.AssetView.as_view()),
]
# ————————02CMDB将服务器基本信息提交到API接口————————
# urls.py

from django.shortcuts import render # Create your views here. # views.py # ————————02CMDB将服务器基本信息提交到API接口————————
import json #轻量级的文本数据交换格式
from django.views import View
from django.views.decorators.csrf import csrf_exempt #和HTML的{% csrf_token %}作用一样
from django.utils.decorators import method_decorator #安全通过 'django.middleware.csrf.CsrfViewMiddleware',
from django.http import JsonResponse#这个类是HttpRespon的子类
class AssetView(View):# http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
@method_decorator(csrf_exempt)#和HTML的{% csrf_token %}作用一样,安全通过 'django.middleware.csrf.CsrfViewMiddleware',
def dispatch(self, request, *args, **kwargs):
return super(AssetView, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):#接受客户端到信息
server_info = json.loads(request.body.decode('utf-8'))
print('获取到的信息: ',type(server_info),server_info)
server_info = json.loads(server_info)#把字符串转换成字典
print('转换后的信息: ',type(server_info),server_info)
hostname = server_info['hostname']
print('主机名',hostname)
ret = {'code': 1000, 'message': '[%s]更新完成' % hostname}#返回到客户端到信息
print(ret)
return JsonResponse(ret)#这个类是HttpRespon的子类
# ————————02CMDB将服务器基本信息提交到API接口————————
# views.py


#settings.py
# ————————01CMDB获取服务器基本信息————————
import os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))##当前路径 # 采集资产的方式,选项有:agent(默认), salt, ssh
MODE = 'agent' # ————————01CMDB获取服务器基本信息———————— # ————————02CMDB将服务器基本信息提交到API接口————————
# 资产信息API
ASSET_API = "http://127.0.0.1:8000/api/asset"
# ————————02CMDB将服务器基本信息提交到API接口————————
#settings.py

# client.py
# ————————01CMDB获取服务器基本信息————————
from src import plugins #__init__.py
from lib.serialize import Json #转成字符串或者模式 # ————————02CMDB将服务器基本信息提交到API接口————————
import requests #伪造页面访问
from config import settings #文件配置
# ————————02CMDB将服务器基本信息提交到API接口———————— class AutoBase(object): # ————————02CMDB将服务器基本信息提交到API接口————————
def __init__(self):
self.asset_api = settings.ASSET_API #ASSET_API = "http://127.0.0.1:8000/api/asset"
def post_asset(self, msg):#post方式向API接口提交资产信息
status = True#获取到信息
try:
response = requests.post(
url=self.asset_api,
json=msg
)
except Exception as e:
response = e
status = False #获取信息时出现错误
print(response.json())
# ————————02CMDB将服务器基本信息提交到API接口———————— def process(self):#派生类需要继承此方法,用于处理请求的入口
raise NotImplementedError('您必须实现过程的方法') class AutoAgent(AutoBase):
def process(self):
server_info = plugins.get_server_info()#获取本地基本信息
server_json = Json.dumps(server_info.data)#json.dumps将 Python 对象编码成 JSON 字符串
print('提交资产信息:',server_json)
# ————————01CMDB获取服务器基本信息———————— # ————————02CMDB将服务器基本信息提交到API接口————————
self.post_asset(server_json)# post方式向接口提交资产信息
# ————————02CMDB将服务器基本信息提交到API接口————————
# client.py


Django项目:CMDB(服务器硬件资产自动采集系统)--02--02CMDB将服务器基本信息提交到API接口的更多相关文章
- Django项目:CMDB(服务器硬件资产自动采集系统)--03--03CMDB信息安全API接口交互认证
#settings.py """ Django settings for AutoCmdb project. Generated by 'django-admin sta ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--12--08CMDB采集硬件数据日志记录
#settings.py # ————————01CMDB获取服务器基本信息———————— import os BASEDIR = os.path.dirname(os.path.dirname(o ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--11--07CMDB文件模式测试采集硬件数据
#settings.py # ————————01CMDB获取服务器基本信息———————— import os BASEDIR = os.path.dirname(os.path.dirname(o ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--06--06CMDB测试Linux系统采集硬件数据的命令01
#base.py # ————————01CMDB获取服务器基本信息———————— from config import settings #配置文件 class BasePlugin(object ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--05--05CMDB采集硬件数据的插件
#__init__.py # ————————05CMDB采集硬件数据的插件———————— from config import settings import importlib # —————— ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--04--04CMDB本地(Agent)模式客户端唯一标识(ID)
# client.py # ————————01CMDB获取服务器基本信息———————— from src import plugins #__init__.py from lib.serializ ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--07--06CMDB测试Linux系统采集硬件数据的命令02
#settings.py """ Django settings for AutoCmdb project. Generated by 'django-admin sta ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--01--01CMDB获取服务器基本信息
AutoClient #settings.py # ————————01CMDB获取服务器基本信息———————— import os BASEDIR = os.path.dirname(os.pat ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--10--06CMDB测试Linux系统采集硬件数据的命令05
cd /py/AutoClient/bin python3 auto-client.py /usr/local/python3/bin/pip install requests python3 aut ...
随机推荐
- PHP缓存技术的应用1
// 自定义缓存类 class Cache_Filesystem { // 缓存写保存 function set ($key, $data, $ttl) { //打开文件为读/写模式 $h = fop ...
- 1002CSP-S模拟测试赛后总结
晚上 我死了.T1全场AC只有我爆零了?? 还非常中二地写了个代码注释: 水题不假,但你不知道题水你更水么?? 碰到简单题就掉以轻心??还告诉自己不要掉以轻心…… 这下是真的滑天下之大稽了吧. 读题不 ...
- BZOJ 2957楼房重建
传送门 线段树 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include< ...
- [BJOI 2018]染色
题意:求01成立. 并查集维护,记录一个变量判断决策. #include<bits/stdc++.h> using namespace std; #define int long long ...
- (2)python tkinter-按钮.标签.文本框、输入框
按钮 无功能按钮 Button的text属性显示按钮上的文本 tkinter.Button(form, text='hello button').pack() 无论怎么变幻窗体大小,永远都在窗体的最上 ...
- POJ-2499-Binary Tree-思维题
Background Binary trees are a common data structure in computer science. In this problem we will loo ...
- JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化
JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化 1使用URLRewrite实现url地址伪静态化1.1URLRewirte的用处 1.满足搜索引擎的要求. 2.隐藏技术实现,提 ...
- 【bzoj 2870】 最长道路tree
题目 边分治 边分和点分相比就是找到一条重心边,考虑所有经过这条边的路径,之后断开这条边分成两个联通块,继续分治 由于每次分治重心是一条边,所以只会产生两个联通块,考虑两个联通块显然要比像点分那样考虑 ...
- NOI 2001 食物链 /// 并查集 oj22035
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1~N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...
- idea存在包但是运行项目一直报java.lang.NoClassDefFoundError的问题
以前做spark时候遇到过,今天高flink又遇到查了半天,这里记录下 1..idea->file->project structure->libraries->(左侧对话框这 ...