如何使用 Python (Windows) 获取所有 IP 地址? [英] How to get all IP addresses using Python (Windows)?

查看:44
本文介绍了如何使用 Python (Windows) 获取所有 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been reading about the following solutions here, but it only works for one IP Address only. I'm not able to print the rest of the IPs (multiple network/wireless cards).

参考资料

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

使用 Python 的 stdlib 查找本地 IP 地址

当我有多个 NIC 时,如何确定我的所有 IP 地址?

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 地址.

Please let me know how to print all IP Addresses in Python.

更新 1:

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

As suggested by Rahul, I've tried the following code but it didn't return anything on the screen.

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

I've also tried Elemag's code as suggested here. It works on Python interpreter but not when I save the code to .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 时它不起作用

It's not working when I save the code into .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-编程/库/netifaces/index.html以及来自模块的信息:https://pypi.org/project/netifaces/

I used the following example from this site as a starting point (changed to work in Python 3): https://yamakira.github.io/python-network-programming/libraries/netifaces/index.html as well as info from the module: 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])

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

which returns interface info in dictionary form:

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

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

as an extension to this, if you do something like:

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 地址:

This would return your IP address in my case:

addr 192.168.0.90

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

Obviously, you can manipulate the dictionary as you see fit, I just added it for information sake

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

希望这对某人有帮助

这篇关于如何使用 Python (Windows) 获取所有 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆