您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。