Django Project: CMDB (server hardware Asset auto-acquisition system)--02--02cmdb to submit the server basic information to the API interface

Source: Internet
Author: User

Autocmdb

1 #urls.py2 """Autocmdb URL Configuration3 4 The ' urlpatterns ' list routes URLs to views. for more information. See:5 https://docs.djangoproject.com/en/2.0/topics/http/urls/6 Examples:7 Function views8 1. Add an import:from my_app import views9 2. Add a URL to Urlpatterns:path (", Views.home, Name= ' home ')Ten class-based views One 1. Add an import:from other_app.views import Home A 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 the 2. Add a URL to Urlpatterns:path (' blog/', include (' Blog.urls ')) - """ -  fromDjango.contribImportAdmin -  fromDjango.urlsImportPath +  - #———————— 02CMDB submits the server basic information to the API interface ———————— +  fromDjango.conf.urlsImportURL A  fromDjango.conf.urlsImportinclude at #———————— 02CMDB submits the server basic information to the API interface ———————— -  -Urlpatterns = [ -Path'admin/', Admin.site.urls), -  -     #———————— 02CMDB submits the server basic information to the API interface ———————— inURL (r'^api/', Include ('Api.urls')), -     #———————— 02CMDB submits the server basic information to the API interface ———————— to  +]
# urls.py

1 #urls.py2 #———————— 02CMDB submits the server basic information to the API interface ————————3  fromDjango.conf.urlsImportURL4  fromApiImport views5 6Urlpatterns = [7URL (r'^asset$', views. Assetview.as_view ()),8 ]9 #———————— 02CMDB submits the server basic information to the API interface ————————
# urls.py

1  fromDjango.shortcutsImportRender2 3 #Create your views here.4 5 #views.py6 7 #———————— 02CMDB submits the server basic information to the API interface ————————8 ImportJson#lightweight Text Data Interchange Format9  fromDjango.viewsImportViewTen  fromDjango.views.decorators.csrfImportCsrf_exempt#same as HTML {% Csrf_token%} One  fromDjango.utils.decoratorsImportMethod_decorator#safe through ' django.middleware.csrf.CsrfViewMiddleware ', A  fromDjango.httpImportJsonresponse#This class is a subclass of Httprespon - classAssetview (View):#http_method_names = [' Get ', ' post ', ' Put ', ' Patch ', ' delete ', ' head ', ' Options ', ' Trace '] -@method_decorator (csrf_exempt)#as with the HTML {% Csrf_token%} function, secure through ' django.middleware.csrf.CsrfViewMiddleware ', the     defDispatch (self, request, *args, * *Kwargs): -         returnSuper (Assetview, self). Dispatch (Request, *args, * *Kwargs) -     defPost (self, request, *args, **kwargs):#Accept client-to-information -Server_info = Json.loads (Request.body.decode ('Utf-8')) +         Print('Information obtained:', type (server_info), Server_info) -Server_info = Json.loads (server_info)#Convert a string to a dictionary +         Print('Post-conversion information:', type (server_info), Server_info) Ahostname = server_info['hostname'] at         Print('Host name', hostname) -RET = {'Code': 1000,'message':'[%s] Update complete'% hostname}#return to the client to the information -         Print(ret) -         returnJsonresponse (ret)#This class is a subclass of Httprespon - #———————— 02CMDB submits the server basic information to the API interface ————————
# views.py

1 #settings.py2 #———————— 01CMDB to get the server basic information ————————3 ImportOS4 5BASEDIR = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)))##当前路径6 7 #the way to collect assets, options are: Agent (default), salt, ssh8MODE ='Agent'9 Ten #———————— 01CMDB to get the server basic information ———————— One  A #———————— 02CMDB submits the server basic information to the API interface ———————— - #Asset Information API -Asset_api ="Http://127.0.0.1:8000/api/asset" the #———————— 02CMDB submits the server basic information to the API interface ————————
#settings. PY

1 #client.py2 #———————— 01CMDB to get the server basic information ————————3  fromSrcImportPlugins#__init__.py4  fromLib.serializeImportJson#turn into a string or pattern5 6 #———————— 02CMDB submits the server basic information to the API interface ————————7 ImportRequests#Forge page Access8  fromConfigImportSettings#File Configuration9 #———————— 02CMDB submits the server basic information to the API interface ————————Ten  One classAutobase (object): A  -     #———————— 02CMDB submits the server basic information to the API interface ———————— -     def __init__(self): theSelf.asset_api = settings. Asset_api#Asset_api = "Http://127.0.0.1:8000/api/asset" -     defPost_asset (Self, msg):#Post method to submit asset information to API interface -Status = True#Get to Information -         Try: +Response =Requests.post ( -Url=Self.asset_api, +json=msg A             ) at         exceptException as E: -Response =e -Status = False#An error occurred while getting information -         Print(Response.json ()) -     #———————— 02CMDB submits the server basic information to the API interface ———————— -  in     defProcess (self):#derived classes need to inherit this method to handle the requested entry -         RaiseNotimplementederror ('How you must implement the process') to  + classautoagent (autobase): -     defprocess (self): theServer_info = Plugins.get_server_info ()#get local basic information *Server_json = Json.dumps (server_info.data)#json.dumps to encode a Python object as a JSON string $         Print('Submit Asset Information:', Server_json)Panax Notoginseng #———————— 01CMDB to get the server basic information ———————— -  the         #———————— 02CMDB submits the server basic information to the API interface ———————— +Self.post_asset (Server_json)#Post method to submit asset information to an interface A         #———————— 02CMDB submits the server basic information to the API interface ————————
# client.py

Django Project: CMDB (server hardware Asset auto-acquisition system)--02--02cmdb to submit the server basic information to the API interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.