简体   繁体   English

如何在 Python 3 中禁用 WIFI? (视窗)

[英]How to disable WIFI in Python 3? (Windows)

I'm making a python script that should turn off the WIFI on windows.我正在制作一个 python 脚本,该脚本应该关闭 windows 上的 WIFI。 I've tried searching on google, but all I could find is to disconnect from a specific WIFI.我试过在谷歌上搜索,但我能找到的只是断开与特定 WIFI 的连接。 And that's not what I want, Because if you on another computer and you run the same script, it probably wont turn off your WIFI because you don't have the same WIFI as the person who wrote the script.这不是我想要的,因为如果您在另一台计算机上运行相同的脚本,它可能不会关闭您的 WIFI,因为您没有与编写脚本的人相同的 WIFI。

So does anybody knows how I can make a python script that will disable\turn off the WIFI on the computer and work with every WIFI without changing the script?那么有人知道我如何制作一个 python 脚本,该脚本将禁用\关闭计算机上的 WIFI 并在不更改脚本的情况下使用每个 WIFI?

Often, high-level programming language don't have access to system settings.通常,高级编程语言无法访问系统设置。 Hence you could import the os module and send out some signal to notify the operating system to switch off the Wifi.因此,您可以导入 os 模块并发送一些信号以通知操作系统关闭 Wifi。

os.system()

you are using Window.您正在使用 Window。 you could fill the function with this你可以用这个填充 function

netsh interface set interface 'Wifi' disabled

For those using OS X, you could fill the function with this对于那些使用 OS X 的人,你可以用这个填充 function

sudo networksetup -setnetworkserviceenabled Wi-Fi off

There isn't a way to reliably do this for any computer and any wifi.对于任何计算机和任何wifi,都没有办法可靠地做到这一点。 Every computer has a different wifi adapter which may be placed differently in the win32 network api.每台计算机都有不同的 wifi 适配器,它们在 win32 网络 api 中的位置可能不同。

What you can do is, execute system commands for command prompt, to query and find the current system's wifi adapter.你可以做的是,在命令提示符下执行系统命令,查询并找到当前系统的wifi适配器。 For example, in order to find the wifi adapter, you could use the following例如,为了找到 wifi 适配器,您可以使用以下命令

 wmic nic get name, index 

This will show you a list of adapters.这将向您显示适配器列表。 If one of them says something at the end like WiFi Adapter , you can use the index of that entry with the following command:如果其中一个在结尾处说WiFi Adapter之类的内容,则可以通过以下命令使用该条目的索引:

wmic path win32_networkadapter where index=7 call disable

This turns the wifi off.这会关闭 wifi。 Keep in mind this may work or not work on different versions of Windows.请记住,这可能适用于不同版本的 Windows。 This also may not work if the Windows user is not using command prompt when running your python script.如果 Windows 用户在运行 python 脚本时未使用命令提示符,这也可能不起作用。

To call these commands from Python, use the subprocess module.要从 Python 调用这些命令,请使用subprocess模块。 You can run commands there with the run() or check_output() methods:您可以使用run()check_output()方法在那里运行命令:

>>> subprocess.run(["dir", "C:\users"], capture_output=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM