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

如何使用Python(Windows)获取所有IP地址?,python,windows,Python,Windows,我一直在阅读以下解决方案,但它只适用于一个IP地址。我无法打印其余IP(多个网卡/无线网卡) 参考资料 C:\>ipconfig| findstr IPv4 IPv4地址………:192.168.1.1 IPv4地址………:192.168.5.1 C:\>python win32上的Python 3.5.1(v3.5.1:37A07CEE59692015年12月6日01:38:48)[MSC v.1900 32位(英特尔)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。

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

参考资料

  • C:\>ipconfig| findstr IPv4
    IPv4地址………:192.168.1.1
    IPv4地址………:192.168.5.1
    C:\>python
    win32上的Python 3.5.1(v3.5.1:37A07CEE59692015年12月6日01:38:48)[MSC v.1900 32位(英特尔)]
    有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
    >>>导入套接字
    >>>打印(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的代码。它在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
    
    c:\Python\code>Python
    win32上的Python 3.5.1(v3.5.1:37A07CEE59692015年12月6日01:38:48)[MSC v.1900 32位(英特尔)]
    有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
    >>>
    >>>[netifaces.ifaddress(iface)[netifaces.AF_INET][0]['addr']用于netifaces.interfaces()中的iface,如果netifa中的netifaces.AF_INET
    ces.ifaddresses(iface)]
    回溯(最近一次呼叫最后一次):
    文件“”,第1行,在
    NameError:未定义名称“netifaces”
    >>>
    >>>导入网络面
    >>>[netifaces.ifaddress(iface)[netifaces.AF_INET][0]['addr']用于netifaces.interfaces()中的iface,如果netifa中的netifaces.AF_INET
    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>
    
    c:\Python\Codes>more test.py
    如果netifaces.AF_INET位于netifaces.interfaces()中,则netifaces.ifAddresss(iface)[netifaces.AF_INET][0]['addr']用于netifaces.interfaces()中的iface。
    IFAddresss(iface)]
    c:\Python\code>test.py
    回溯(最近一次呼叫最后一次):
    文件“c:\Python\Codes\test.py”,第1行,在
    [netifaces.ifaddress(iface)[netifaces.AF_INET][0]['addr']用于netifaces.interfaces()中的iface,如果netifa中的netifaces.AF_INET
    ces.ifaddresses(iface)]
    NameError:未定义名称“netifaces”
    c:\Python\code>
    c:\Python\code>more test.py
    导入网络面
    如果netifaces.AF_INET位于netifaces.interfaces()中,则netifaces.ifAddresss(iface)[netifaces.AF_INET][0]['addr']用于netifaces.interfaces()中的iface。
    IFAddresss(iface)]
    c:\Python\code>
    c:\Python\code>test.py
    c:\Python\code>
    

    参考:

    我从这个站点使用了以下示例作为起点(改为使用Python 3): 以及来自模块的信息:

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

    [{'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
    
    很明显,你可以根据自己的喜好操作字典,我只是为了提供信息而添加了它

    • 在Windows10Python3.6上测试
    • 在linux python 2.7、3.6上测试

    希望这对某人有所帮助

    谢谢@Rahul,我尝试了代码,但没有发生任何事情
    c:\Python\Codes>netifaces导入接口的更多ip.py,ifaddress,AF_INET def ip4_addresses():ip_list=[]对于接口中的接口():对于ifaddress(接口)[AF INET]:ip_list.append(link['addr'])返回ip_列表c:\Python\code>ip.py c:\Python\code>
    使用
    打印(ip4_地址())
    打印输出。我已经更新了我的代码,看看吧。谢谢你没有工作<代码>打印(ip4_地址())导致以下错误
    c:\Python\code>ip.py回溯(最近一次调用):文件“c:\Python\code\ip.py”,第8行,打印(ip4_地址())文件“c:\Python\code\ip.py”,第5行,ifaddresses(接口)中链接的ip4_地址[AF_INET]:keyrerror:2 c:\Python\code>
    1。没有返回屏幕的
    概念。2.您甚至不在任何地方调用函数
    ip\u address()
    。你的程序基本上什么都不做。谢谢@ElmoVanKielmo,我添加了
    打印(ip4\U地址)
    ,但没有得到我期望的输出。我该如何解决这个问题?
    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)
    
    addr 192.168.0.90