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

问题描述 投票:0回答:2

我这里一直在阅读以下解决方案,但它仅适用于一个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:

我也按照建议here尝试了Elemag的代码。它适用于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 windows
2个回答
1
投票
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
print(ip4_addresses())

参考:How do I determine all of my IP addresses when I have multiple NICs?


0
投票

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

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

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

[{'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'}]

作为对此的延伸,如果您执行以下操作:

    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中测试过

希望这有助于某人

© www.soinside.com 2019 - 2024. All rights reserved.