我正在尝试使用proposal python模块与wpa连接到wifi。在API中,我发现了一个名为wpa_supplicant的函数,它看起来应该可以帮助我与wifi AP关联,但我不理解输入参数的含义。
输入参数称为所需网络的D-bus对象路径。 谷歌搜索后,我找不到任何关于使用此功能的例子。
更新
我使用d-feet来查看/ fi / w1 / wpasupplicant1下的对象路径。 如下所示:
/fi/w1/wpa_supplicant1
/fi/w1/wpa_supplicant1/interfaces/7
/fi/w1/wpa_supplicant1/interfaces/7/BSSs/14
/fi/w1/wpa_supplicant1/interfaces/7/Networks/0
/fi/w1/wpa_supplicant1/interfaces/7/Networks/1
更新-1
还是不知道如何处理select_network这个功能。 但是我找到了以下链接:
此链接描述了如何使用dbus模块dircetly连接到wifi。
任何提示都会受到欢迎。
答案 0 :(得分:1)
此外,对于那些努力使其工作的人,您需要通过添加-u(启用dbus)来启用wpa_supplicant应用程序守护程序上的DBUS功能
关于如何使用它的快速示例,只是一个粗略的编程示例。
from wpa_supplicant.core import WpaSupplicantDriver
from twisted.internet.selectreactor import SelectReactor
import threading
import time
import errno
import sys
import types
import netifaces
import dbus
class PythonWifiScanner:
wifiAccessPoints = []
def __init__(self,reactor):
self._reactor = reactor
threading.Thread(target=self._reactor.run, kwargs={'installSignalHandlers': 0}).start()
time.sleep(0.2) # let reactor start
self.driver = WpaSupplicantDriver(reactor)
self.supplicant = self.driver.connect()
# get network interfaces
self.net_iface = netifaces.interfaces()
def get_configured_networks(self,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode()).get_networks()
def get_single_wpa_interface(self,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode())
def get_interfaces(self):
return self.net_iface
def select_network(self,network_path,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode()).select_network(network_path)
def add_network(self,network_cfg,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode()).add_network(network_cfg)
def scan_interface_for_networks(self,interfaceNumber):
# Get interface and scan the network
interface = self.supplicant.get_interface(self.net_iface[interfaceNumber].decode())
wifiNetworks = interface.scan(block=True)
self.wifiAccessPoints = []
for singleWifi in wifiNetworks:
self.wifiAccessPoints.append(singleWifi.get_ssid())
return wifiNetworks
# Start a simple Twisted SelectReactor
sample_network_cfg = {}
sample_network_cfg['psk'] = "EnterYourKeyHere"
sample_network_cfg['ssid'] = "EnterYourWifiHere"
sample_network_cfg['key_mgmt'] = "WPA-PSK"
reactor = SelectReactor()
dave=PythonWifiScanner(reactor)
value = None
bus = dbus.SystemBus()
print "Interface:" + dave.get_interfaces()[3]
# scan for available networks
for singleWifi in dave.scan_interface_for_networks(3):
print "Wifi SSID:" + singleWifi.get_ssid()
print "Wifi Network Type:" + singleWifi.get_network_type()
# Add network configuration to wpa_supplicant
configpath = dave.add_network(sample_network_cfg,3)
# Attach and Select your network (will need to setip address)
dave.select_network(configpath.get_path(),3)
reactor.stop()