簡體   English   中英

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

[英]How to get all IP addresses using Python (Windows)?

我一直在這里閱讀以下解決方案,但它僅適用於一個 IP 地址。 我無法打印其余的 IP(多個網絡/無線卡)。

參考文獻

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

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

  3. 當我有多個 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 地址。

更新 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

當我將代碼保存到 .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])

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

[{'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 上測試

希望這有助於某人

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())

參考: 當我有多個 NIC 時,如何確定我的所有 IP 地址?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM