查找我在Python中连接到的所有网络适配器的脚本 [英] Script to find all network adapters I'm connected to in Python
问题描述
我有一台笔记本电脑,该笔记本电脑使用一个或多个网络适配器连接到组织的网络.我不确定如何启动该程序,也不确定应该在哪里寻找本机python方法或其他资源.
I have a laptop that is connected to my organization's network using one or more network adapters. I'm not sure how to start this or where I should look for native python methods or other resources.
我想知道如何找到所有使用python连接的网络适配器或NIC.我想尝试在没有第三方库的情况下完成此操作,但是如果我必须这样做也可以.
I was wondering how can I discover all the network adapters or NICs I am connected to using python. I want to try to accomplish this without a 3rd party library but if I have to that is fine as well.
我最终希望能够编写一个能够连续监视每个网络的连接状态和连接质量的工具.但是,我首先必须找出要连接的网络适配器和数量.
I ultimately want to be able to write a tool that will continuously monitor the connectivity status and connection quality of each network. However I first have to find out which and how many network adapters I'm connected to.
推荐答案
一段时间以来,一直困扰着我的是,没有一种简单的方法可以从Python获取机器网络接口的地址. [...]
It's been annoying me for some time that there's no easy way to get the address(es) of the machine's network interfaces from Python. [...]
此软件包试图解决该问题.
This package attempts to solve that problem.
一些相关示例:
>>> netifaces.interfaces()
['lo0', 'gif0', 'stf0', 'en0', 'en1', 'fw0']
>>> netifaces.ifaddresses('lo0')
{18: [{'addr': ''}],
2: [{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}],
30: [{'peer': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'addr': '::1'}, {'peer': '', 'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::1%lo0'}]}
通过pip install netifaces
获取.
这篇关于查找我在Python中连接到的所有网络适配器的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!