python获取网络速度,,以及cpu


 
   Ajax Test
   
     
        
        
        
        
   
 
 
   


import psutil as psutil
from django.shortcuts import render
from django.views import generic
from django.http import HttpResponse
from django.shortcuts import render_to_response
import json,time

class hanshu():
    ln=[]
    def data(request, id):
        rlist2 = []
        n = psutil.net_io_counters()
        s1=n.bytes_sent # 发送字节数
        s2=n.bytes_recv  # 接受字节数
        time.sleep(1)
        T=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))+''
        n2=psutil.net_io_counters()
        s1=round((n2.bytes_sent-s1)/1024,2)
        s2=round((n2.bytes_recv-s2)/1024,2)
        # 本机cpu的总占用率
        v = psutil.cpu_percent(0)
        rlist2.append({"zhan": psutil.virtual_memory().percent, "cpu": v,'up':s1,'down':s2,'time':T})
        print(rlist2)
        rjson = json.dumps(rlist2)
        response = HttpResponse()
        response['Content-Type'] = "text/javascript"
        response.write(rjson)
        return response


    def update(request):
        return render_to_response('update.html')

你可能感兴趣的:(python获取网络速度,,以及cpu)