python获取所有网卡IPv4地址

参见:http://www.malaoshi.top/show_1EF3BTIm7Vtv.html

获取IPv4地址

指定IPv4族

import socket

def getips():
    ips=[]
    #获取本机电脑名
    hostname = socket.gethostname()
    addrs = socket.getaddrinfo(hostname,None,family=socket.AF_INET)
    for item in addrs:
        ips.append(item[4][0])
    return ips

ips=getips()
print(ips)

结果:

['192.168.56.1', '172.20.10.4']

原文出处:/show_1EF3BTJQubpw.html