Python script capture server data is submitted to the Django Web server via API and then displayed on the page

Source: Internet
Author: User
Tags django web server python script

Python program that collects information about the server

#coding: Utf-8

Import Psutil

Import Urllib

Import Urllib.request

Import Urllib.parse

#import URLLIB2

Import requests


#获取数据

Cpudata = Psutil.cpu_times ()

#处理数据

Cpumax = Cpudata.user+cpudata.system+cpudata.idle

cpuused = Cpudata.user+cpudata.system

Cpuuse = Cpuused/cpumax


#将数据进行打包

data = {"cpuused": Cpuuse}

#sendData = Urllib.parse.urlencode (data)

#sendData = Senddata.encode (' Utf-8 ')

url = "http://192.168.52.129:8000/api/savecpu/"

#定义请求头部

Header = {

"User-agent": "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/56.0.2924.87 safari/537.36 ",

}


#定义请求

Res =requests.post (url,data = data)

#req = urllib.request.Request (url,data = senddata,headers = Header,method = ' POST ')

#添加Header

#req. Add_header (headers = header)

#发送请求

#ope = Urllib.request.urlopen (req)

#打印结果

Print (Res.content.decode ())

#print (Ope.read ())



Django creates a project, creates an app, and I've created an API app here

urls.py file configuration information for the API


From Django.conf.urls import URL

From. Import views


Urlpatterns = [

URL (r ' ^index/$ ', views.index,name= ' index '),

# URL (R ' ^getinfo/$ ', views.getinfo,name= ' GetInfo '),

URL (r "^savecpu/$", Views.savecpu,name= ' savecpu '),

URL (r "^showcpu/$", Views.show,name= ' showcpu '),


]



View File

2 functions defined


def savecpu (Request):

"""

This method is our interface method,

The requested data is stored in the database when requested

"""

Statue = {}

if Request.method = = "POST" and request. Post: #检测请求的方式是post, and the POST request has data

#request. Post This method holds all the data for the current POST request in the form of a dictionary

cpuused = Request. post["cpuused"] #使用类字典的取值方式取出传递上来的cpu使用率

Times = Datetime.datetime.now () #获取当前时间

Cpudatabase = SAVECPU () #实例化一个SaveCpu的模型实例

cpudatabase.used = cpuused #将数据赋值给模型

Cpudatabase.time = Times #同上

Cpudatabase.save () #将模型映射到数据库

statue["statue"] = "Success" #定义当前接口状态为保存成功

Else

statue["statue"] = "Request method must be post" #定义当前接口状态为失败

Return Jsonresponse (statue) #将接口状态返回给请求者


Def show (Request):

CPU = SaveCpu.objects.all ()

return render (Request, "api/showdata.html", locals ())

# Create your views here.


The agent side (Python program) constantly collects the information of the native server, passes the data to the Web server side (Django) through the API, the Web server side receives the data which the interface transmits, the data which passes through in the format saves the data in the database, Extracting data from a database, displaying data in front-end pages


Probably a simple server-"agent end is like this



Python script capture server data is submitted to the Django Web server via API and then displayed on the page

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.