Complete the task:
1, API authentication.
1 ImportJSON2 fromDjango.shortcutsImportRender, HttpResponse3 fromDjango.views.decorators.csrfImportcsrf_exempt4 from. PluginsImportPluginmanger5 fromDatetimeImportDate6 fromRepositoryImportModels7 fromDjango.db.modelsImportQ8 ImportHashlib9 Import TimeTen One A defMD5 (ARG): -HS =hashlib.md5 () -Hs.update (Arg.encode ('Utf-8')) the returnhs.hexdigest () - - -Key ="Asdasodoquqwejqweo" +Visited_keys = { - } + A at defApi_auth (func): - defInner (request, *args, * *Kwargs): -Server_float_ctime =time.time () -Auth_header_val = Request. Meta.get ('Http_auth_api') - #841770f74ef3b7867d90be37c5b4adfc|1506571253.9937866 -Client_md5_str, client_ctime = Auth_header_val.split ('|', Maxsplit=1) inClient_float_ctime =float (client_ctime) - to #First off + if(Client_float_ctime +) <Server_float_ctime: - returnHttpResponse ('time verification timed out, your hand is not fast enough') the * #Second pass: $SERVER_MD5_STR = MD5 ("%s|%s"%(Key, Client_ctime,))Panax Notoginseng ifServer_md5_str! =Client_md5_str: - returnHttpResponse ('Validation not successful') the + #Third Pass: A ifVisited_keys.get (CLIENT_MD5_STR): the returnHttpResponse ('someone's been here already.') + -VISITED_KEYS[CLIENT_MD5_STR] =Client_float_ctime $ returnFunc (Request, *args, * *Kwargs) $ - returnInner - the -@csrf_exempt#no longer validates CSRFWuyi defServer (Request): the ifRequest.method = ="GET": -Current_date =Date.today () Wu #get a list of hosts not collected today -Host_list =models. Server.objects.filter ( AboutQ (q (latest_date=none) | Q (latest_date__date__lt=current_date)) & Q (server_status_id=2) $). VALUES ('hostname') -Host_list =list (host_list) - Print(host_list) - returnHttpResponse (Json.dumps (host_list)) A ifRequest.method = ="POST": + #latest asset data submitted by customer service theServer_dict = Json.loads (Request.body.decode ('Utf-8')) - $ if notserver_dict['Basic']['Status']: the returnHttpResponse ("I can't do that, my concubine.") the theManager =Pluginmanger () theResponse = Manager.exec(server_dict) - in returnHttpResponse (Json.dumps (response)) the the About @api_auth the defTest (Request): the returnHttpResponse ('Normal User')
cmdb_server/views.py
1 #-*-coding:utf-8-*-2 #__author__ = "Maple"3 ImportRequests4 Import Time5 ImportHashlib6 7 8 defMD5 (ARG):9HS =hashlib.md5 ()TenHs.update (Arg.encode ('Utf-8')) One returnhs.hexdigest () A - -Key ="Asdasodoquqwejqweo" theCTime =Str (time.time ()) -New_key ="%s|%s"% (key, CTime,)#Random string | time stamp -MD5_STR = MD5 (New_key)#MD5 Encryption - +Auth_header_val ="%s|%s"%(Md5_str, CTime,) - +Response = Requests.get ('http://127.0.0.1:8000/api/test.html', headers={'Auth-api': Auth_header_val}) A Print(Response.text)
cmdb_client/test.py
Development of CMDB Hardware information Management system (III.)