python-公共IP的传出带宽 [英] python - outgoing bandwidth for public ips

查看:61
本文介绍了python-公共IP的传出带宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码行来检查从centos框中发送的总字节数

I am using the below line of code to check the total bytes sent out of my centos box

import psutil
psutil.net_io_counters().bytes_sent

我只想为公共IP执行此操作。换句话说,我只想计算公共IP的传出带宽。

I want to perform this just for the public IP. In other words, I want to calculate outgoing bandwidth just for public IP.

推荐答案

import ipaddress
import psutil

net = psutil.net_io_counters(pernic=True)

for name, interface in psutil.net_if_addrs().items():
    for address in interface:
        try:
            network = ipaddress.IPv4Network(f'{address.address}\{address.netmask}')
            if not network.is_private and not network.is_reserved:
                print(net[name].bytes_sent)
                break
        except ValueError as e:
            # these would be eg MAC addresses or similar
            pass

这篇关于python-公共IP的传出带宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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