Before doing system resource escalation, because do not know how to escalate the data to the server to show, and then use a most direct and unsafe way, directly write to the database, this is inconvenient, mainly because of the need to authorize and do the relevant firewall policy (if there are hundreds of thousands of clients, even in the form of network segments, Also need to repeat the operation), and if the information of the database changes, need to all the client database information to modify, more trouble; recently learned a URL way to pass data to the server, the implementation of simple API functions, simple examples are as follows:
Client code:
#!/usr/bin/env python#coding:utf8import jsonimport platformimport psutilimport urllib , urllib2from multiprocessing import cpu_countsys_info={}sys_info[' System '] = [] sys_info[' CPU '] = []sys_info[' mem '] = [] sys_info[' disk '] = []sys_info[' WIP '] = [] #系统信息system =sys_info[' System '].append (Platform.uname () [0]) #cpu个数cpu_count =sys_info[' CPU ']. Append (Cpu_count ()) #内存mem = psutil.virtual_memory () mem_info = sys_info[' mem '].append ( mem.total * 1 / (1024**3)) #磁盘空间sdiskusage = psutil.disk_usage ('/') disk_info =sys_info[' disk '].append (sdiskusage.total * 1 / (1024**3)) #外网ipdef pub_ip (): url = "http://ip.cip.cc" wip = Urllib2.urlopen (URL). ReadLine (). Rstrip () return wipsys_info[' WIP '].append ( PUB_IP ()) if __name__ == '__main__ ': print sys_info print json.dumps (Sys_info, Indent=4,ensure_ascii=false) data = urllib.urlencode (query=sys_info) print data respose = urllib.urlopen (url= "/http 127.0.0.1:8090/serveradd/", data=data)
Server-side code (Django):
urls.py settings
URL (r ' ^serveradd/$ ', ' App.views.serveradd '),
View Code (views.py)
Def serveradd (req): if req.method == ' GET ': print "get -----Error" elif req.method == ' POST ': print "Post method" request_dict = req. post print request_dict system = request_dict.get (' system ') wip = request_dict.get (' WIP ') data = {K: v for k, v in request_dict.items ()} host = host.objects.filter (WIP=WIP). First () if not host: &Nbsp; host = host (**data) host.save (force_ Insert=true) data = { ' msg ': ' OK ', ' Status ': ' OK } return httpresponse (data) #结果POST method<querydict: {u ' mem ': [u ' [4L] '], u ' CPU ': [u ' [4] '], u ' disk ': [u ' [232] '], u ' system ': [u ' [' Darwin '] '], u ' WIP ': [u ' [' 12.196.9.193 '] "]}>----[' Darwin ']----[' 12.196.9.193 ']
This article from the "Day Up goto" blog, please be sure to keep this source http://ttxsgoto.blog.51cto.com/4943095/1786210
Implementation of the Simple API