使用Python获取本机所有IP地址

来源:互联网 发布:清华大学研究生知乎 编辑:程序博客网 时间:2024/06/05 02:03

使用Python获取本机所有IP地址

#!/usr/bin/env python# coding=gbk# This program is optimized for Python 2.7.import socketlocalIP = socket.gethostbyname(socket.gethostname())print "Local IP:"print "    %s" % localIPipList = socket.gethostbyname_ex(socket.gethostname())for i in ipList:    # 过滤空序列、主机名和localIP(localIP不是业务地址发情况)    if i and (not isinstance(i, str)) and (i != localIP):          print "External IP:"        for ip in i:            print "    %s" % ip
0 0
原创粉丝点击