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

<html>
 <head>
   <title>Ajax Test</title>
   <script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
     <script src="https://code.highcharts.com.cn/highcharts/highcharts.js"></script>
        <script src="https://code.highcharts.com.cn/highcharts/modules/exporting.js"></script>
        <script src="https://code.highcharts.com.cn/highcharts/modules/series-label.js"></script>
        <script src="https://code.highcharts.com.cn/highcharts/modules/oldie.js"></script>
        <script src="https://code.highcharts.com.cn/highcharts-plugins/highcharts-zh_CN.js"></script>
   <script type="text/javascript" language="javascript">
            i=0
            v1=new Array()
            v2=new Array()
            v3=new Array()
            v4=new Array()
            v5=new Array()
           function update(id) {

               $.getJSON("/data/" + id + "/",function(data) {
                $.each(data, function(){
                    v1[i]=this.zhan;
                    v2[i]=this.cpu;
                    v3[i]=this.up;
                    v4[i]=this.down;
                    v5[i]=this.time;
                    i++;

            var chart = Highcharts.chart('container1', {
                title: {
                             text: '系统资源使用'
                    },
                legend: {
                            layout: 'vertical',
                            align: 'right',
                            verticalAlign: 'middle'
                    },
                xAxis: {
                        categories: v5
                    },
                yAxis: {
                            title: {
                                text: '占比'
                            },
                            labels: {
                                formatter: function () {
                                    return this.value + '%';
                                }
                            }
                        },
                    plotOptions: {
                            series: {
                                    label: {
                                            connectorAllowed: false
                                    },

                            }
                    },
                    series: [{
                            name: 'cpu',
                            data: v2
                    }, {
                            name: '内存占用',
                            data: v1
                    }],
                    responsive: {
                            rules: [{
                                    condition: {
                                            maxWidth: 500
                                    },
                                    chartOptions: {
                                            legend: {
                                                    layout: 'horizontal',
                                                    align: 'center',
                                                    verticalAlign: 'bottom'
                                            }
                                    }
                            }]
                    }
                    });
             var chart = Highcharts.chart('container2', {
                    title: {
                             text: '网络状况'
                    },
                    legend: {
                            layout: 'vertical',
                            align: 'right',
                            verticalAlign: 'middle'
                    },
                    xAxis: {
                        categories: v5
                    },
                    yAxis: {
                            title: {
                                text: '速度'
                            },
                            labels: {
                                formatter: function () {
                                    return this.value + 'kb';
                                }
                            }
                        },
                    plotOptions: {
                            series: {
                                    label: {
                                            connectorAllowed: false
                                    },

                            }
                    },
                    series: [{
                            name: '上传速度',
                            data: v3
                    }, {
                            name: '下载速度',
                            data: v4
                    }],
                    responsive: {
                            rules: [{
                                    condition: {
                                            maxWidth: 500
                                    },
                                    chartOptions: {
                                            legend: {
                                                    layout: 'horizontal',
                                                    align: 'center',
                                                    verticalAlign: 'bottom'
                                            }
                                    }
                            }]
                    }
                    });
                });
               });
           }
           function timeDown(limit, i) {
               limit--;
               if (i > 2) {
                   i = 0;
               }
               if (limit < 0) {
                   limit = 3;
                   update(i);
                   i++;
               }
               $('#time').text(limit + '秒后刷新');
               setTimeout(function() {
                   timeDown(limit, i);
               }, 1000)
           }

           $(document).ready(function() {
               timeDown(3, 0)
        })
   </script>
 </head>
 <body>
   <p id="time"></p>
   <div id="container1"></div>
 <div id="container2"></div>
 </body>
</html>

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')
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值