python获得上传/下载速度 [英] python getting upload/download speeds

查看:546
本文介绍了python获得上传/下载速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的计算机上监视上传和下载速度.名为conky的程序已经在conky conf中执行了以下操作:

I want to monitor on my computer the upload and download speeds. A program called conky already does it with the following in conky conf:

Connection quality: $alignr ${wireless_link_qual_perc wlan0}%
${downspeedgraph wlan0}
DLS:${downspeed wlan0} kb/s $alignr total: ${totaldown wlan0}

它显示了我浏览时几乎实时的速度.我希望能够使用python访问相同的信息.

and it shows me the speeds in almost real time while I browse. I want to be able to access the same information using python.

推荐答案

您可以根据设备的rx_bytes和tx_bytes自己计算速度,并在一段时间内轮询这些值

You can calculate the speed yourself based on the rx_bytes and tx_bytes for the device and polling those values over an interval

这是我使用Python 3破解的一个非常简单的解决方案

Here is a very simplistic solution I hacked together using Python 3

#!/usr/bin/python3

import time

def get_bytes(t, iface='wlan0'):
    with open('/sys/class/net/' + iface + '/statistics/' + t + '_bytes', 'r') as f:
        data = f.read();
    return int(data)

if __name__ == '__main__':
    (tx_prev, rx_prev) = (0, 0)

    while(True):
        tx = get_bytes('tx')
        rx = get_bytes('rx')

        if tx_prev > 0:
            tx_speed = tx - tx_prev
            print('TX: ', tx_speed, 'bps')

        if rx_prev > 0:
            rx_speed = rx - rx_prev
            print('RX: ', rx_speed, 'bps')

        time.sleep(1)

        tx_prev = tx
        rx_prev = rx

这篇关于python获得上传/下载速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆