Python-获取 IP 地址 发表于 2017-07-22 | 分类于 Python | | Python 获取 IP 地址。 123456789101112131415161718import osimport redef valid_ip(ip): if ("255" in ip) or ( ip == "127.0.0.1") or ( ip == "0.0.0.0" ): return False else: return Truedef get_ip(valid_ip): ipss = ''.join(os.popen("ifconfig").readlines()) match = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ips = re.findall(match, ipss, flags=re.M) ip = filter(valid_ip, ips) return ''.join(ip)ip_addr = get_ip(valid_ip)print ip_addr