1. net_if_addrs()
Return the addresses associated to each NIC (network interface
card) installed on the system as a dictionary whose keys are the
NIC names and value is a list of namedtuples for each address
assigned to the NIC. Each namedtuple includes 5 fields:
- family
- address
- netmask
- broadcast
- ptp
'family' can be either socket.AF_INET, socket.AF_INET6 or
psutil.AF_LINK, which refers to a MAC address.
'address' is the primary address and it is always set.
'netmask' and 'broadcast' and 'ptp' may be None.
'ptp' stands for "point to point" and references the destination
address on a point to point interface (typically a VPN).
'broadcast' and 'ptp' are mutually exclusive.
Note: you can have more than one address of the same family
associated with each interface.
示例:
tun0 : [snic(family=2, address='192.168.8.1', netmask='255.255.255.255', broadcast=None, ptp='192.168.8.2'), snic(family=10, address='fe80::f586:cb28:87d1:436f%tun0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)]
vmnet1 : [snic(family=2, address='192.168.247.1', netmask='255.255.255.0', broadcast='192.168.247.255', ptp=None), snic(family=10, address='fe80::250:56ff:fec0:1%vmnet1', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snic(family=17, address='00:50:56:c0:00:01', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
lo : [snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snic(family=10, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)]
enp8s0f0 : [snic(family=17, address='00:1b:cd:05:05:24', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
enp8s0f1 : [snic(family=17, address='00:1b:cd:05:05:25', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
vmnet8 : [snic(family=2, address='172.16.194.1', netmask='255.255.255.0', broadcast='172.16.194.255', ptp=None), snic(family=10, address='fe80::250:56ff:fec0:8%vmnet8', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snic(family=17, address='00:50:56:c0:00:08', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
enp4s0f0 : [snic(family=2, address='10.0.110.34', netmask='255.255.255.0', broadcast='10.0.110.255', ptp=None), snic(family=10, address='fe80::ec4:7aff:fe16:2ef0%enp4s0f0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snic(family=17, address='0c:c4:7a:16:2e:f0', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
virbr0 : [snic(family=2, address='192.168.122.1', netmask='255.255.255.0', broadcast='192.168.122.255', ptp=None), snic(family=17, address='52:54:00:46:82:b6', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
virbr0-nic : [snic(family=17, address='52:54:00:46:82:b6', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
enp4s0f1 : [snic(family=17, address='0c:c4:7a:16:2e:f1', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]
参数:
family, 示例中的值分别有2, 10, 17, 分别表示AF_INET, AF_INET6, AF_LINK,可以看到这3类对应的address类型分别是ipv4, ipv6, mac
ptp,
2. net_if_stats()
Return information about each NIC (network interface card)
installed on the system as a dictionary whose keys are the
NIC names and value is a namedtuple with the following fields:
- isup: whether the interface is up (bool)
- duplex: can be either NIC_DUPLEX_FULL, NIC_DUPLEX_HALF or
NIC_DUPLEX_UNKNOWN
- speed: the NIC speed expressed in mega bits (MB); if it can't
be determined (e.g. 'localhost') it will be set to 0.
- mtu: the maximum transmission unit expressed in bytes.
3. net_io_counters()
Return network I/O statistics as a namedtuple including
the following fields:
- bytes_sent: number of bytes sent
- bytes_recv: number of bytes received
- packets_sent: number of packets sent
- packets_recv: number of packets received
- errin: total number of errors while receiving
- errout: total number of errors while sending
- dropin: total number of incoming packets which were dropped
- dropout: total number of outgoing packets which were dropped
(always 0 on OSX and BSD)
If pernic is True return the same information for every
network interface installed on the system as a dictionary
with network interface names as the keys and the namedtuple
described above as the values.

本文介绍了如何使用psutil库获取系统网络接口的详细信息,包括net_if_addrs()函数返回的地址信息,net_if_stats()提供的网络接口状态,如速度、双工模式和MTU,以及net_io_counters()统计的网络I/O数据。
5081

被折叠的 条评论
为什么被折叠?



