本篇內容主要講解“怎么用python獲取網(wǎng)卡與ip”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用python獲取網(wǎng)卡與ip”吧!
我們提供的服務有:成都網(wǎng)站建設、網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、金堂縣ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的金堂縣網(wǎng)站制作公司
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獲取網(wǎng)卡與ip”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!