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

时间:2022-03-22 07:38:18

I have multiple Network Interface Cards on my computer, each with its own IP address.

我的计算机上有多个网络接口卡,每个都有自己的IP地址。

When I use gethostbyname(gethostname()) from Python's (built-in) socket module, it will only return one of them. How do I get the others?

当我从Python的(内置)套接字模块使用gethostbyname(gethostname())时,它只会返回其中一个。我如何得到其他人?

10 个解决方案

#1


39  

Use the netifaces module. Because networking is complex, using netifaces can be a little tricky, but here's how to do what you want:

使用netifaces模块。因为网络很复杂,使用netifaces可能有点棘手,但这里是如何做你想要的:

>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0']
>>> netifaces.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:11:2f:32:63:45'}], 2: [{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::211:2fff:fe32:6345%eth0'}]}
>>> for interface in netifaces.interfaces():
...   print netifaces.ifaddresses(interface)[netifaces.AF_INET]
...
[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
[{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}]
>>> for interface in netifaces.interfaces():
...   for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
...     print link['addr']
...
127.0.0.1
10.0.0.2

This can be made a little more readable like this:

这样可以更加可读:

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

If you want IPv6 addresses, use AF_INET6 instead of AF_INET. If you're wondering why netifaces uses lists and dictionaries all over the place, it's because a single computer can have multiple NICs, and each NIC can have multiple addresses, and each address has its own set of options.

如果需要IPv6地址,请使用AF_INET6而不是AF_INET。如果您想知道为什么netifaces在整个地方使用列表和词典,那是因为一台计算机可以有多个NIC,每个NIC可以有多个地址,每个地址都有自己的一组选项。

#2


8  

All addresses in one line with the help of the netifaces module:

在netifaces模块的帮助下,所有地址都在一行中:

[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.ifaddresses(iface)]

#3


7  

import socket
[i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)]

#4


4  

Just for completeness, another option would be to use psutil.

只是为了完整性,另一种选择是使用psutil。

tldr;

import socket
import psutil

def get_ip_addresses(family):
    for interface, snics in psutil.net_if_addrs().items():
        for snic in snics:
            if snic.family == family:
                yield (interface, snic.address)

ipv4s = list(get_ip_addresses(socket.AF_INET))
ipv6s = list(get_ip_addresses(socket.AF_INET6))

Explanation

The function you need is net_if_addrs. I.e.:

您需要的功能是net_if_addrs。即:

import psutil
psutil.net_if_addrs()

Which results in something like this (Python 3):

结果是这样的(Python 3):

{'br-ae4880aa80cf': [snic(family=<AddressFamily.AF_INET: 2>, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.0.1', ptp=None),
                     snic(family=<AddressFamily.AF_PACKET: 17>, address='02:42:e5:ae:39:94', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snic(family=<AddressFamily.AF_INET: 2>, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.0.1', ptp=None),
             snic(family=<AddressFamily.AF_PACKET: 17>, address='02:42:38:d2:4d:77', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'eno1': [snic(family=<AddressFamily.AF_PACKET: 17>, address='54:be:f7:0b:cf:a9', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'lo': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
        snic(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)],
 'wlp2s0': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.1.4', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
            snic(family=<AddressFamily.AF_PACKET: 17>, address='00:21:27:ee:d6:03', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

(Python 2):

(Python 2):

{'br-ae4880aa80cf': [snic(family=2, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.0.1', ptp=None),
                     snic(family=17, address='02:42:e5:ae:39:94', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snic(family=2, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.0.1', ptp=None),
             snic(family=17, address='02:42:38:d2:4d:77', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'eno1': [snic(family=17, address='54:be:f7:0b:cf:a9', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'lo': [snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
        snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)],
 'wlp2s0': [snic(family=2, address='192.168.1.4', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
            snic(family=17, address='00:21:27:ee:d6:03', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

Note: Since you can have more than one address of the same family associated with each interface, the dict values are lists.

注意:由于每个接口可以有多个相同系列的地址,因此dict值是列表。

Each snic is a namedtuple which includes 5 fields:

每个snic都是一个包含5个字段的namedtuple:

  • family: the address family, either AF_INET, AF_INET6 or psutil.AF_LINK, which refers to a MAC address.
  • family:地址族,AF_INET,AF_INET6或psutil.AF_LINK,指MAC地址。
  • address: the primary NIC address (always set).
  • address:主NIC地址(始终设置)。
  • netmask: the netmask address (may be None).
  • netmask:网络掩码地址(可以是None)。
  • broadcast: the broadcast address (may be None).
  • 广播:广播地址(可以是无)。
  • ptp: stands for “point to point”; it’s the destination address on a point to point interface (typically a VPN). broadcast and ptp are mutually exclusive (may be None).
  • ptp:代表“点对点”;它是点对点接口(通常是VPN)上的目标地址。 broadcast和ptp是互斥的(可以是None)。

#5


2  

https://docs.python.org/3.4/library/socket.html#socket.if_nameindex

https://docs.python.org/3.4/library/socket.html#socket.if_nameindex

socket.if_nameindex()

socket.if_nameindex()

Return a list of network interface information (index int, name string) tuples. OSError if the system call fails.

返回网络接口信息列表(索引int,名称字符串)元组。如果系统调用失败,则为OSError。

Availability: Unix.

可用性:Unix。

New in version 3.3.

版本3.3中的新功能。


made this code that is runable on Python 3.4, UNIX / Linux

使这个代码可以在Python 3.4,UNIX / Linux上运行

#!/env/python3.4
import socket
import fcntl
import struct

def active_nic_addresses():
    """
    Return a list of IPv4 addresses that are active on the computer.
    """

    addresses = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1]

    return addresses

def get_ip_address( NICname ):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', NICname[:15].encode("UTF-8"))
    )[20:24])


def nic_info():
    """
    Return a list with tuples containing NIC and IPv4
    """
    nic = []

    for ix in socket.if_nameindex():
        name = ix[1]
        ip = get_ip_address( name )

        nic.append( (name, ip) )

    return nic

if __name__ == "__main__":

    print( active_nic_addresses() )
    print( nic_info() )

Will print something like:

将打印像:

['192.168.0.2']
[('lo', '127.0.0.1'), ('enp3s0', '192.168.0.2')]

#6


1  

It's linux only, but there's a very simple recipe here http://code.activestate.com/recipes/439094/

它只是linux,但这里有一个非常简单的食谱http://code.activestate.com/recipes/439094/

It probably uses similar code to the netifaces package mentioned in another answer (but current version linked here)

它可能使用与另一个答案中提到的netifaces包类似的代码(但此处链接的当前版本)

The socket.getaddrinfo() doesn't actually return the bound ip address for the device. If your hosts file contains a line with "127.0.1.1 yourhost.example.com yourhost", which is a common configuration, getaddrinfo is only going to return 127.0.1.1.

socket.getaddrinfo()实际上并不返回设备的绑定IP地址。如果您的hosts文件包含一行“127.0.1.1 yourhost.example.com yourhost”(这是一个常见配置),则getaddrinfo将仅返回127.0.1.1。

#7


1  

Here is a routine for finding all IPv4 and IPv6 interfaces. As a previous poster pointed out, socket.gethostbyname_ex() does not work for IPv6, and the Python documentation recommends one use socket.getaddressinfo() instead.

以下是查找所有IPv4和IPv6接口的例程。正如之前的海报指出的那样,socket.gethostbyname_ex()不适用于IPv6,而Python文档建议使用socket.getaddressinfo()代替。

This routine adds the callback IPv4 interface (127.0.0.1), and if there are any IPv6 interfaces then it also adds the callback IPv6 interface (::1). On my machine, socket.getaddrinfo() will give me one or both of these but only if I have no other interfaces available.

此例程添加了回调IPv4接口(127.0.0.1),如果有任何IPv6接口,则它还会添加回调IPv6接口(:: 1)。在我的机器上,socket.getaddrinfo()将给我一个或两个,但前提是我没有其他可用的接口。

For my needs, I wanted to try to open a UDP socket on a specified port on each of my available interfaces, which is why the code has "port" and socket.SOCK_DGRAM in it. It is safe to change those, e.g. if you don't have a port in mind.

根据我的需要,我想尝试在每个可用接口上的指定端口上打开UDP套接字,这就是代码中包含“port”和socket.SOCK_DGRAM的原因。改变它们是安全的,例如如果你没有一个端口。

addrinfo_ipv4 = socket.getaddrinfo(hostname,port,socket.AF_INET,socket.SOCK_DGRAM)
addrinfo_ipv6 = []
try:
    addrinfo_ipv6 = socket.getaddrinfo(hostname,port,socket.AF_INET6,socket.SOCK_DGRAM)
except socket.gaierror:
    pass
addrinfo = [(f,t,a) for f,t,p,cn,a in addrinfo_ipv4+addrinfo_ipv6]
addrinfo_local = [(socket.AF_INET,socket.SOCK_DGRAM,('127.0.0.1',port))]
if addrinfo_ipv6: 
    addrinfo_local.append( (socket.AF_INET6,socket.SOCK_DGRAM,('::1',port)) )
[addrinfo.append(ai) for ai in addrinfo_local if ai not in addrinfo]

#8


1  

This snippet will give a list of all available IPV4 addresses in the system.

此代码段将提供系统中所有可用IPV4地址的列表。

import itertools
from netifaces import interfaces, ifaddresses, AF_INET

links = filter(None, (ifaddresses(x).get(AF_INET) for x in interfaces()))
links = itertools.chain(*links)
ip_addresses = [x['addr'] for x in links]

#9


0  

You should directly obtain all IP configured IP addresses, e.g. by running ifconfig and parsing its output (it's also possible to do what ifconfig does directly in Python, see how it is done in C). If you want host names, use gethostbyaddr.

您应该直接获取所有IP配置的IP地址,例如通过运行ifconfig并解析其输出(也可以直接在Python中执行ifconfig,看看它是如何在C中完成的)。如果需要主机名,请使用gethostbyaddr。

#10


0  

You can do it fairly easily like this:

你可以这样轻松地做到这一点:

import netifaces

for interface in netifaces.interfaces():
    print netifaces.ifaddresses(interface)

For more information you can look up the netifaces documentation.

有关更多信息,您可以查看netifaces文档。

#1


39  

Use the netifaces module. Because networking is complex, using netifaces can be a little tricky, but here's how to do what you want:

使用netifaces模块。因为网络很复杂,使用netifaces可能有点棘手,但这里是如何做你想要的:

>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0']
>>> netifaces.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:11:2f:32:63:45'}], 2: [{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::211:2fff:fe32:6345%eth0'}]}
>>> for interface in netifaces.interfaces():
...   print netifaces.ifaddresses(interface)[netifaces.AF_INET]
...
[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
[{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}]
>>> for interface in netifaces.interfaces():
...   for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
...     print link['addr']
...
127.0.0.1
10.0.0.2

This can be made a little more readable like this:

这样可以更加可读:

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

If you want IPv6 addresses, use AF_INET6 instead of AF_INET. If you're wondering why netifaces uses lists and dictionaries all over the place, it's because a single computer can have multiple NICs, and each NIC can have multiple addresses, and each address has its own set of options.

如果需要IPv6地址,请使用AF_INET6而不是AF_INET。如果您想知道为什么netifaces在整个地方使用列表和词典,那是因为一台计算机可以有多个NIC,每个NIC可以有多个地址,每个地址都有自己的一组选项。

#2


8  

All addresses in one line with the help of the netifaces module:

在netifaces模块的帮助下,所有地址都在一行中:

[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.ifaddresses(iface)]

#3


7  

import socket
[i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)]

#4


4  

Just for completeness, another option would be to use psutil.

只是为了完整性,另一种选择是使用psutil。

tldr;

import socket
import psutil

def get_ip_addresses(family):
    for interface, snics in psutil.net_if_addrs().items():
        for snic in snics:
            if snic.family == family:
                yield (interface, snic.address)

ipv4s = list(get_ip_addresses(socket.AF_INET))
ipv6s = list(get_ip_addresses(socket.AF_INET6))

Explanation

The function you need is net_if_addrs. I.e.:

您需要的功能是net_if_addrs。即:

import psutil
psutil.net_if_addrs()

Which results in something like this (Python 3):

结果是这样的(Python 3):

{'br-ae4880aa80cf': [snic(family=<AddressFamily.AF_INET: 2>, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.0.1', ptp=None),
                     snic(family=<AddressFamily.AF_PACKET: 17>, address='02:42:e5:ae:39:94', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snic(family=<AddressFamily.AF_INET: 2>, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.0.1', ptp=None),
             snic(family=<AddressFamily.AF_PACKET: 17>, address='02:42:38:d2:4d:77', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'eno1': [snic(family=<AddressFamily.AF_PACKET: 17>, address='54:be:f7:0b:cf:a9', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'lo': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
        snic(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)],
 'wlp2s0': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.1.4', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
            snic(family=<AddressFamily.AF_PACKET: 17>, address='00:21:27:ee:d6:03', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

(Python 2):

(Python 2):

{'br-ae4880aa80cf': [snic(family=2, address='172.18.0.1', netmask='255.255.0.0', broadcast='172.18.0.1', ptp=None),
                     snic(family=17, address='02:42:e5:ae:39:94', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'docker0': [snic(family=2, address='172.17.0.1', netmask='255.255.0.0', broadcast='172.17.0.1', ptp=None),
             snic(family=17, address='02:42:38:d2:4d:77', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'eno1': [snic(family=17, address='54:be:f7:0b:cf:a9', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
 'lo': [snic(family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
        snic(family=17, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)],
 'wlp2s0': [snic(family=2, address='192.168.1.4', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
            snic(family=17, address='00:21:27:ee:d6:03', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}

Note: Since you can have more than one address of the same family associated with each interface, the dict values are lists.

注意:由于每个接口可以有多个相同系列的地址,因此dict值是列表。

Each snic is a namedtuple which includes 5 fields:

每个snic都是一个包含5个字段的namedtuple:

  • family: the address family, either AF_INET, AF_INET6 or psutil.AF_LINK, which refers to a MAC address.
  • family:地址族,AF_INET,AF_INET6或psutil.AF_LINK,指MAC地址。
  • address: the primary NIC address (always set).
  • address:主NIC地址(始终设置)。
  • netmask: the netmask address (may be None).
  • netmask:网络掩码地址(可以是None)。
  • broadcast: the broadcast address (may be None).
  • 广播:广播地址(可以是无)。
  • ptp: stands for “point to point”; it’s the destination address on a point to point interface (typically a VPN). broadcast and ptp are mutually exclusive (may be None).
  • ptp:代表“点对点”;它是点对点接口(通常是VPN)上的目标地址。 broadcast和ptp是互斥的(可以是None)。

#5


2  

https://docs.python.org/3.4/library/socket.html#socket.if_nameindex

https://docs.python.org/3.4/library/socket.html#socket.if_nameindex

socket.if_nameindex()

socket.if_nameindex()

Return a list of network interface information (index int, name string) tuples. OSError if the system call fails.

返回网络接口信息列表(索引int,名称字符串)元组。如果系统调用失败,则为OSError。

Availability: Unix.

可用性:Unix。

New in version 3.3.

版本3.3中的新功能。


made this code that is runable on Python 3.4, UNIX / Linux

使这个代码可以在Python 3.4,UNIX / Linux上运行

#!/env/python3.4
import socket
import fcntl
import struct

def active_nic_addresses():
    """
    Return a list of IPv4 addresses that are active on the computer.
    """

    addresses = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1]

    return addresses

def get_ip_address( NICname ):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', NICname[:15].encode("UTF-8"))
    )[20:24])


def nic_info():
    """
    Return a list with tuples containing NIC and IPv4
    """
    nic = []

    for ix in socket.if_nameindex():
        name = ix[1]
        ip = get_ip_address( name )

        nic.append( (name, ip) )

    return nic

if __name__ == "__main__":

    print( active_nic_addresses() )
    print( nic_info() )

Will print something like:

将打印像:

['192.168.0.2']
[('lo', '127.0.0.1'), ('enp3s0', '192.168.0.2')]

#6


1  

It's linux only, but there's a very simple recipe here http://code.activestate.com/recipes/439094/

它只是linux,但这里有一个非常简单的食谱http://code.activestate.com/recipes/439094/

It probably uses similar code to the netifaces package mentioned in another answer (but current version linked here)

它可能使用与另一个答案中提到的netifaces包类似的代码(但此处链接的当前版本)

The socket.getaddrinfo() doesn't actually return the bound ip address for the device. If your hosts file contains a line with "127.0.1.1 yourhost.example.com yourhost", which is a common configuration, getaddrinfo is only going to return 127.0.1.1.

socket.getaddrinfo()实际上并不返回设备的绑定IP地址。如果您的hosts文件包含一行“127.0.1.1 yourhost.example.com yourhost”(这是一个常见配置),则getaddrinfo将仅返回127.0.1.1。

#7


1  

Here is a routine for finding all IPv4 and IPv6 interfaces. As a previous poster pointed out, socket.gethostbyname_ex() does not work for IPv6, and the Python documentation recommends one use socket.getaddressinfo() instead.

以下是查找所有IPv4和IPv6接口的例程。正如之前的海报指出的那样,socket.gethostbyname_ex()不适用于IPv6,而Python文档建议使用socket.getaddressinfo()代替。

This routine adds the callback IPv4 interface (127.0.0.1), and if there are any IPv6 interfaces then it also adds the callback IPv6 interface (::1). On my machine, socket.getaddrinfo() will give me one or both of these but only if I have no other interfaces available.

此例程添加了回调IPv4接口(127.0.0.1),如果有任何IPv6接口,则它还会添加回调IPv6接口(:: 1)。在我的机器上,socket.getaddrinfo()将给我一个或两个,但前提是我没有其他可用的接口。

For my needs, I wanted to try to open a UDP socket on a specified port on each of my available interfaces, which is why the code has "port" and socket.SOCK_DGRAM in it. It is safe to change those, e.g. if you don't have a port in mind.

根据我的需要,我想尝试在每个可用接口上的指定端口上打开UDP套接字,这就是代码中包含“port”和socket.SOCK_DGRAM的原因。改变它们是安全的,例如如果你没有一个端口。

addrinfo_ipv4 = socket.getaddrinfo(hostname,port,socket.AF_INET,socket.SOCK_DGRAM)
addrinfo_ipv6 = []
try:
    addrinfo_ipv6 = socket.getaddrinfo(hostname,port,socket.AF_INET6,socket.SOCK_DGRAM)
except socket.gaierror:
    pass
addrinfo = [(f,t,a) for f,t,p,cn,a in addrinfo_ipv4+addrinfo_ipv6]
addrinfo_local = [(socket.AF_INET,socket.SOCK_DGRAM,('127.0.0.1',port))]
if addrinfo_ipv6: 
    addrinfo_local.append( (socket.AF_INET6,socket.SOCK_DGRAM,('::1',port)) )
[addrinfo.append(ai) for ai in addrinfo_local if ai not in addrinfo]

#8


1  

This snippet will give a list of all available IPV4 addresses in the system.

此代码段将提供系统中所有可用IPV4地址的列表。

import itertools
from netifaces import interfaces, ifaddresses, AF_INET

links = filter(None, (ifaddresses(x).get(AF_INET) for x in interfaces()))
links = itertools.chain(*links)
ip_addresses = [x['addr'] for x in links]

#9


0  

You should directly obtain all IP configured IP addresses, e.g. by running ifconfig and parsing its output (it's also possible to do what ifconfig does directly in Python, see how it is done in C). If you want host names, use gethostbyaddr.

您应该直接获取所有IP配置的IP地址,例如通过运行ifconfig并解析其输出(也可以直接在Python中执行ifconfig,看看它是如何在C中完成的)。如果需要主机名,请使用gethostbyaddr。

#10


0  

You can do it fairly easily like this:

你可以这样轻松地做到这一点:

import netifaces

for interface in netifaces.interfaces():
    print netifaces.ifaddresses(interface)

For more information you can look up the netifaces documentation.

有关更多信息,您可以查看netifaces文档。