python - 如何使用Python(Windows)获取所有IP地址?

标签 python windows

我在这里阅读了以下解决方案,但它仅适用于一个 IP 地址。我无法打印其余的 IP(多个网络/无线卡)。

引用文献

  1. http://net-informations.com/python/net/ipadress.htm

  2. Finding local IP addresses using Python's stdlib

  3. How do I determine all of my IP addresses when I have multiple NICs?

C:\>ipconfig | findstr IPv4
   IPv4 Address. . . . . . . . . . . : 192.168.1.1
   IPv4 Address. . . . . . . . . . . : 192.168.5.1

C:\>python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> print (socket.gethostbyname(socket.gethostname()))
192.168.5.1
>>>

请告诉我如何在 Python 中打印所有 IP 地址。

更新1:

按照 Rahul 的建议,我尝试了以下代码,但屏幕上没有返回任何内容。

c:\Python\Codes>more ip.py
from netifaces import interfaces, ifaddresses, AF_INET

def ip4_addresses():
    ip_list = []
    for interface in interfaces():
        for link in ifaddresses(interface)[AF_INET]:
            ip_list.append(link['addr'])
    return ip_list

c:\Python\Codes>

c:\Python\Codes>ip.py

c:\Python\Codes>

更新2:

我还按照建议尝试了 Elemag 的代码 here 。它适用于 Python 解释器,但当我将代码保存到 .py 时则不起作用

c:\Python\Codes>python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
ces.ifaddresses(iface)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'netifaces' is not defined
>>>
>>> import netifaces
>>> [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
ces.ifaddresses(iface)]
['192.168.1.10', '192.168.56.1', '127.0.0.1']
>>>
>>> ^Z

当我将代码保存到.py中时它不起作用

c:\Python\Codes>more test.py
[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.
ifaddresses(iface)]

c:\Python\Codes>test.py
Traceback (most recent call last):
  File "c:\Python\Codes\test.py", line 1, in <module>
    [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
ces.ifaddresses(iface)]
NameError: name 'netifaces' is not defined

c:\Python\Codes>

c:\Python\Codes>more test.py
import netifaces
[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.
ifaddresses(iface)]

c:\Python\Codes>

c:\Python\Codes>test.py

c:\Python\Codes>

最佳答案

我使用此站点中的以下示例作为起点(更改为在 Python 3 中工作): https://yamakira.github.io/python-network-programming/libraries/netifaces/index.html 以及来自模块的信息: https://pypi.org/project/netifaces/

import netifaces
for iface in netifaces.interfaces():
  iface_details = netifaces.ifaddresses(iface)
  if netifaces.AF_INET in iface_details:
    print(iface_details[netifaces.AF_INET])

以字典形式返回接口(interface)信息:

[{'addr': '192.168.0.90', 'netmask': '255.255.255.0', 'broadcast': '192.168.0.255'}]
[{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'broadcast': '127.255.255.255'}]

作为对此的扩展,如果您执行以下操作:

import netifaces
for iface in netifaces.interfaces():
    iface_details = netifaces.ifaddresses(iface)
    if netifaces.AF_INET in iface_details:
        print(iface_details[netifaces.AF_INET])
        for ip_interfaces in iface_details[netifaces.AF_INET]:
            for key, ip_add in ip_interfaces.items():
                if key == 'addr' and ip_add != '127.0.0.1':
                    print(key, ip_add)

在我的例子中,这将返回您的 IP 地址:

addr 192.168.0.90

显然,您可以根据需要操作字典,我只是为了提供信息而添加它

  • 在 Windows 10 python 3.6 上测试
  • 在 Linux python 2.7、3.6 上测试

希望这对某人有帮助

关于python - 如何使用Python(Windows)获取所有IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49195864/

相关文章:

python - Minimax 算法 tic tac toe 不工作

python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声?

c++ - 在线程错误 C2064 : term does not evaluate to a function taking 0 arguments

c++ - 使用 Shobjidl.h 的 Microsoft SDK 中的奇怪错误

Windows 批处理 - 删除隐藏文件

python - 对数间隔值低于 1

python - os.environ 具有不存在的键/环境变量

python - Base64解码

c - 按名称获取 C 中的进程 ID

windows - (Windows)系统文件缓存磁盘回写失败会出现什么错误?他们是如何被报道的?