连接WiFi python的最简单方法
Posted
技术标签:
【中文标题】连接WiFi python的最简单方法【英文标题】:Simplest way to connect WiFi python 【发布时间】:2019-06-26 00:31:18 【问题描述】:我想使用 python 连接到我的 wifi 网络。我知道网络的 SSID 和密钥,并且它在 WPA2 安全性中加密。我见过一些图书馆,比如无线和 pywifi,但第一个没有用,第二个太复杂了。连接wifi最简单的方法是什么?最好的图书馆/方式是什么?
我使用无线库失败的代码(我当然是通过 pip 安装的):
from wireless import Wireless
wire = Wireless()
wire.connect(ssid='myhome',password='password')
解释器输出:
Traceback (most recent call last):
File "C:/Users/Aviv/PycharmProjects/Networks/WiFi/1/1.py", line 4, in
<module>
wire = Wireless()
File "C:\Python27\lib\site-packages\wireless\Wireless.py", line 23, in
__init__
self._driver_name = self._detectDriver()
File "C:\Python27\lib\site-packages\wireless\Wireless.py", line 50, in
_detectDriver
compare = self.vercmp(ver, "0.9.9.0")
File "C:\Python27\lib\site-packages\wireless\Wireless.py", line 71, in vercmp
return cmp(normalize(actual), normalize(test))
File "C:\Python27\lib\site-packages\wireless\Wireless.py", line 70, in
normalize
return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")]
ValueError: invalid literal for int() with base 10: 'file'
【问题讨论】:
您能说明您尝试了什么以及遇到了什么错误吗? 我在Ubuntu 16.04
上试过你的代码,它工作正常!
可能问题出在您的无线驱动程序中,而不是您的代码中
【参考方案1】:
无需任何模块即可轻松连接 Wifi:
import os
class Finder:
def __init__(self, *args, **kwargs):
self.server_name = kwargs['server_name']
self.password = kwargs['password']
self.interface_name = kwargs['interface']
self.main_dict =
def run(self):
command = """sudo iwlist wlp2s0 scan | grep -ioE 'ssid:"(.*.*)'"""
result = os.popen(command.format(self.server_name))
result = list(result)
if "Device or resource busy" in result:
return None
else:
ssid_list = [item.lstrip('SSID:').strip('"\n') for item in result]
print("Successfully get ssids ".format(str(ssid_list)))
for name in ssid_list:
try:
result = self.connection(name)
except Exception as exp:
print("Couldn't connect to name : . ".format(name, exp))
else:
if result:
print("Successfully connected to ".format(name))
def connection(self, name):
cmd = "nmcli d wifi connect password iface ".format(name,
self.password,
self.interface_name)
try:
if os.system(cmd) != 0: # This will run the command and check connection
raise Exception()
except:
raise # Not Connected
else:
return True # Connected
if __name__ == "__main__":
# Server_name is a case insensitive string, and/or regex pattern which demonstrates
# the name of targeted WIFI device or a unique part of it.
server_name = "example_name"
password = "your_password"
interface_name = "your_interface_name" # i. e wlp2s0
F = Finder(server_name=server_name,
password=password,
interface=interface_name)
F.run()
【讨论】:
【参考方案2】:我希望您使用的是 Windows 机器。然后你应该安装 winwifi 模块。 在安装 winwifi 模块之前,最好先安装 plumbum 模块。 尝试在python 32位版本中安装。
点击这里安装铅模块:https://pypi.org/project/plumbum/
然后安装winwifi:https://pypi.org/project/winwifi/
现在您可以尝试使用以下代码连接已知的 wifi:
import winwifi
winwifi.WinWiFi.connect('ssid_of_the_router')
这个模块有更多的命令,比如:
winwifi.WinWiFi.disconnect()
winwifi.WinWiFi.addprofile('ssid_of_new_router')等
您可以通过浏览我的 github 存储库来检查这一点: https://github.com/ashiq-firoz/winwifi_Guide
【讨论】:
以上是关于连接WiFi python的最简单方法的主要内容,如果未能解决你的问题,请参考以下文章