本篇内容主要讲解“怎么用python获取网卡与ip”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用python获取网卡与ip”吧!
def get_interface_ip(ifname): sckt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( sckt.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24])
def _get_interface_list(): """Provides a list of available network interfaces as a list of tuples (name, ip)""" max_iface = 32 # Maximum number of interfaces(Aribtrary) bytes = max_iface * 32 is_32bit = (8 * struct.calcsize("P")) == 32 # Set Architecture struct_size = 32 if is_32bit else 40 try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) names = array.array('B', '\0' * bytes) outbytes = struct.unpack('iL', fcntl.ioctl( s.fileno(), 0x8912, # SIOCGIFCONF struct.pack('iL', bytes, names.buffer_info()[0]) ))[0] namestr = names.tostring() return [(namestr[i:i + 32].split('\0', 1)[0], socket.inet_ntoa(namestr[i + 20:i + 24]))\ for i in range(0, outbytes, struct_size)] except IOError: raise NetworkError('Unable to call ioctl with SIOCGIFCONF')
到此,相信大家对“怎么用python获取网卡与ip”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。