如何在 Windows 上连接到 WiFi 网络?
问题描述
我正在尝试用 Python 3 编写脚本,但目前所有可用的模块都可以在 Python 2 上运行,这将使我能够搜索无线网络并连接到它们.有没有为此的 Python 3 库?
I am trying to write a script in Python 3 but all modules available today work on python 2 which will enable me to search for wireless networks and to connect to them. Is there any Python 3 library for this?
我为 python 2 尝试的代码
The code I tried for python 2
from wireless import Wireless
wireless = Wireless()
wireless.connect(ssid='ssid', password='password')
这给了我一个错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:UsersHimanshu PoddarAppDataLocalProgramsPythonPython36-32libsite-packageswirelessWireless.py", line 23, in __init__
self._driver_name = self._detectDriver()
File "C:UsersHimanshu PoddarAppDataLocalProgramsPythonPython36-32libsite-packageswirelessWireless.py", line 50, in _detectDriver
compare = self.vercmp(ver, "0.9.9.0")
File "C:UsersHimanshu PoddarAppDataLocalProgramsPythonPython36-32libsite-packageswirelessWireless.py", line 71, in vercmp
return cmp(normalize(actual), normalize(test))
NameError: name 'cmp' is not defined
但这不起作用,因为它基于 python 2.有什么方法可以使用 Python 3 连接到 wifi
But this is not working since it is based on python 2. Is there any way to connect to a wifi using Python 3
解决方案
在windows中使用python连接wifi,更好的选择是使用winwifi模块:
To connect wifi using python in windows, the better option is to use the winwifi module:
我建议你在安装 winwifi 之前先安装 plumbum.这是下载铅的链接:https://pypi.org/project/plumbum/
I recommend you to install plumbum before installing winwifi. This is the link to download plumbum: https://pypi.org/project/plumbum/
之后从这里安装winwifi:https://pypi.org/project/winwifi/最好安装在32位的python文件夹中.
After this install winwifi from here:https://pypi.org/project/winwifi/ It's better to install it in 32-bit python folder.
安装后可以通过以下代码查看模块(这是连接之前连接设备的路由器)
:
After installing you could check the module by the following code (This is to connect router which was connected to the device before)
:
import winwifi
winwifi.WinWiFi.connect('the_exact_ssid_or_name_of_your_known_wifi_router')
在您的 IDLE 上运行此代码时,您可以看到 wifi 已连接到您的设备.如果您想连接新设备,您可以在添加配置文件后使用代码:
On running this code on your IDLE, you could see that the wifi is connected to your device. If you want to connect a new device you could use the code after adding profile:
import winwifi
winwifi.WinWiFi.addprofile('ssid_of_router')
winwifi.WinWiFi.connect('the_ssid_of_router', 'password')
您可以使用以下命令断开当前 Wifi:
You can disconnect the current Wifi using the command:
import winwifi
winwifi.WinWiFi.disconnect()
这个模块还有更多的命令,尝试探索它们.更多内容请参考 winwifi 文件夹中的 main.py 文件.
There are many more commands on this module, try to explore them. Just refer to the main.py file in winwifi folder for many more.
相关文章