自动获取本机ip和mac
目的:执行系统命令,获取执行结果
subprocess.Popen('ipconfig/all',shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
问题:不使用-w参数就可以运行,但是会弹出终端窗口,加上-w参数无法正常运行
解决:问题关键在于当去掉终端窗口时,程序没有等待命令执行结果就退出了,所以添加等待就可以了
p.wait()
最后使用命令打包: pyinstaller -F -i xx.ico -n name test.py --noconsole
import re import subprocess # 获取本机mac和ip地址
def get_mac(): info = {} try: p= subprocess.Popen('ipconfig/all',shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() mystr = [i.decode('gbk') for i in p.stdout.readlines()] for i in range(len(mystr)): mac = re.findall(r'([0-9a-fA-F]{2}([/\s:-][0-9a-fA-F]{2}){5})', mystr[i]) if mac: for j in range(i + 1, len(mystr)): ip = re.findall(r'IPv4 地址[\s\S]+?(\d+.\d+.\d+.\d+)', mystr[j]) mac2 = re.findall(r'([0-9a-fA-F]{2}([/\s:-][0-9a-fA-F]{2}){5})', mystr[j]) if ip: info['ip_mac'] = (ip[0], mac[0][0]) i = j break if mac2: i = j break if info: break except Exception as e: pass