本文主要研究的是使用Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址,分享了相關(guān)的實(shí)例代碼,具體介紹如下。
成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括大城網(wǎng)站建設(shè)、大城網(wǎng)站制作、大城網(wǎng)頁制作以及大城網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,大城網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到大城省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!搜了一天,竟然沒找到一段合適的代碼來獲取機(jī)器中所有網(wǎng)卡的ip,掩碼和廣播地址,大部分都是用socket,但是socket通常返回的要不就是內(nèi)網(wǎng)地址,要不就是公網(wǎng)地址,不能夠找到所有地址,真的太憂桑了,決定自己通過ifconfig或ipconfig的返回信息,一步步地過濾了。這次的代碼主要用到了正則表達(dá)式和subprocess模塊,而且為了兼容所有平臺(tái)(win,linux和mac),也用到了platform來判斷系統(tǒng)類型,不說太多,代碼如下:
import subprocess import re import platform def find_all_ip(platform): ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}' if platform == "Darwin" or platform == "Linux": ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE) output = ipconfig_process.stdout.read() ip_pattern = re.compile('(inet %s)' % ipstr) if platform == "Linux": ip_pattern = re.compile('(inet addr:%s)' % ipstr) pattern = re.compile(ipstr) iplist = [] for ipaddr in re.finditer(ip_pattern, str(output)): ip = pattern.search(ipaddr.group()) if ip.group() != "127.0.0.1": iplist.append(ip.group()) return iplist elif platform == "Windows": ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE) output = ipconfig_process.stdout.read() ip_pattern = re.compile("IPv4 Address(\. )*: %s" % ipstr) pattern = re.compile(ipstr) iplist = [] for ipaddr in re.finditer(ip_pattern, str(output)): ip = pattern.search(ipaddr.group()) if ip.group() != "127.0.0.1": iplist.append(ip.group()) return iplist def find_all_mask(platform): ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}' maskstr = '0x([0-9a-f]{8})' if platform == "Darwin" or platform == "Linux": ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE) output = ipconfig_process.stdout.read() mask_pattern = re.compile('(netmask %s)' % maskstr) pattern = re.compile(maskstr) if platform == "Linux": mask_pattern = re.compile(r'Mask:%s' % ipstr) pattern = re.compile(ipstr) masklist = [] for maskaddr in mask_pattern.finditer(str(output)): mask = pattern.search(maskaddr.group()) if mask.group() != '0xff000000' and mask.group() != '255.0.0.0': masklist.append(mask.group()) return masklist elif platform == "Windows": ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE) output = ipconfig_process.stdout.read() mask_pattern = re.compile(r"Subnet Mask (\. )*: %s" % ipstr) pattern = re.compile(ipstr) masklist = [] for maskaddr in mask_pattern.finditer(str(output)): mask = pattern.search(maskaddr.group()) if mask.group() != '255.0.0.0': masklist.append(mask.group()) return masklist def get_broad_addr(ipstr, maskstr): iptokens = map(int, ipstr.split(".")) masktokens = map(int, maskstr.split(".")) broadlist = [] for i in range(len(iptokens)): ip = iptokens[i] mask = masktokens[i] broad = ip & mask | (~mask & 255) broadlist.append(broad) return '.'.join(map(str, broadlist)) def find_all_broad(platform): ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}' if platform == "Darwin" or platform == "Linux": ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE) output = (ipconfig_process.stdout.read()) broad_pattern = re.compile('(broadcast %s)' % ipstr) if platform == "Linux": broad_pattern = re.compile(r'Bcast:%s' % ipstr) pattern = re.compile(ipstr) broadlist = [] for broadaddr in broad_pattern.finditer(str(output)): broad = pattern.search(broadaddr.group()) broadlist.append(broad.group()) return broadlist elif platform == "Windows": iplist = find_all_ip(platform) masklist = find_all_mask(platform) broadlist = [] for i in range(len(iplist)): broadlist.append(get_broad_addr(iplist[i], masklist[i])) return broadlist system = platform.system() print(find_all_ip(system)) print(find_all_mask(system)) print(find_all_broad(system))
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。