Implementation of the Simple API

Source: Internet
Author: User

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

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.