python执行cmd命令——控制电脑连接wifi——程序打包

import subprocess
# 使用Popen创建进程,并与进程进行复杂的交互
proc = subprocess.Popen(
    'netsh wlan show network', # cmd特定的查询空间的命令
    stdin=None, # 标准输入 键盘
    stdout=subprocess.PIPE, # -1 标准输出(演示器、终端) 保存到管道中以便进行操作
    stderr=subprocess.PIPE, # 标准错误,保存到管道
    shell=True)
outinfo, errinfo = proc.communicate() # 获取输出和错误信息
print(outinfo.decode('gbk')) # 外部程序 (windows系统)决定编码格式
print(errinfo.decode('gbk'))

控制电脑连接wifi

import pywifi
import time

wifi = pywifi.PyWiFi()
ifaces = wifi.interfaces()[0]               #返回的是无线网卡的地址


print(ifaces)
print(ifaces.status())    #连接状态 貌似连接上是 4 没连接时候是 0
print(ifaces.name())

###################以下注释掉的可以启用或者参考##################
# ifaces.scan()                      #扫描周围的wifi
# time.sleep(1)
# basewifi = ifaces.scan_results()    #获取可连接的wifi列表信息
#
# for i in basewifi:
#     print("wifi scan result:{}".format(i.ssid))           #打印出周围的
#     print("wifi device MAC address:{}".format(i.bssid))
###############################################################


##################连接指定的wifi##########################
print(ifaces.name())  # 输出无线网卡名称
ifaces.disconnect()     #断开连接
time.sleep(3)

profile = pywifi.Profile()  # 配置文件
profile.ssid = "SHUNCOM-00F6ED"  # wifi名称
profile.key = ""  # wifi密码

ifaces.remove_all_network_profiles()  # 删除其它配置文件
tmp_profile = ifaces.add_network_profile(profile)  # 加载配置文件
ifaces.connect(tmp_profile)          #开始连接wifi
time.sleep(5)
isok = True

time.sleep(1)


'''
Pyinstaller -F py_word.py 打包exe
 
Pyinstaller -F -w py_word.py 不带控制台的打包
 
Pyinstaller -F -w -i chengzi.ico py_word.py 打包指定exe图标打包
'''

你可能感兴趣的:(python,电脑,开发语言)