Turn WiFi off using Python on Windows 10?

别等时光非礼了梦想. 提交于 2019-12-08 10:38:01

问题


I have been looking for a way to toggle my wifi on and off using a script. I figured this could be done maybe by entering airplane mode or some other method. When I was googleing for the answer though I could not find anything useful for windows, just for android and even macOS.

Anyone have a 2 functions, one for turning wifi off, and one for turning it back on? Or connecting/disconnecting from a specific one works too. It is my default wifi if this is relevant.


回答1:


Python does not have direct access to your Wifi Adapter but windows does. So you can use os module to run commmand prompt codes and control your wifi adapter.

  1. Get the interface name

  2. Then you can just use the name to run on/off commands

    # Get the interface name using this script.
    import os 
    os.system("netsh interface show interface")
    

Then replace it with Wifi in this script to get going.

import os 
def enable():
    os.system("netsh interface set interface 'Wifi' enabled")

def disable():
    os.system("netsh interface set interface 'Wifi' disabled")      



回答2:


All you need to do is take the space out and it works for me.

For example: use interface name = "Wi-Fi2" with the code above.



来源:https://stackoverflow.com/questions/44246527/turn-wifi-off-using-python-on-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!