python下载速度显示_python获得上传/下载速度

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}

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.

解决方案

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
make /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xproto.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/bigreq.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xc_misc.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/composite.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/damage.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/dpms.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/dri2.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/glx.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/randr.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/record.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/render.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/res.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/screensaver.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/shape.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/shm.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/sync.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xevie.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xf86dri.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xfixes.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xinerama.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xinput.xml Traceback (most recent call last): File "./c_client.py", line 1039, in <module> module.register() File "/usr/lib/python2.7/dist-packages/xcbgen/state.py", line 93, in register matcher.execute(self, self.namespace) File "/usr/lib/python2.7/dist-packages/xcbgen/matcher.py", line 115, in execute funcs[elt.tag](elt, module, namespace) KeyError: 'eventstruct' make: *** [Makefile:1018: xinput.c] Error 1
05-24

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值