使用wifi库的回溯(最近一次通话)

时间:2018-10-16 13:05:46

标签: python-3.x wifi

当我尝试连接到wifi网络时,我已使用wifi库在python 3中编写了一个代码,出现此错误:

'Traceback (most recent call last):
File "WifiModuleLibrary.py", line 102, in <module>
print(Connect('uth-open2'))
File "WifiModuleLibrary.py", line 41, in Connect
savedcell.activate()
File "/usr/local/lib/python3.6/dist-packages/wifi/scheme.py", line 172, in activate
subprocess.check_output(['/sbin/ifdown', self.interface], stderr=subprocess.STDOUT)
File "/usr/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlan0']' returned non-zero exit status 1.

在调试代码方面我不太擅长:

import wifi

def Search():
    wifilist = []

    cells = wifi.Cell.all('wlan0')

    for cell in cells:
        wifilist.append(cell)

    return wifilist


def FindFromSearchList(ssid):
    wifilist = Search()

    for cell in wifilist:
        if cell.ssid == ssid:
            return cell

    return False


def FindFromSavedList(ssid):
    cell = wifi.Scheme.find('wlan0', ssid)

    if cell:
        return cell

    return False


def Connect(ssid, password=None):
    cell = FindFromSearchList(ssid)

    if cell:
        savedcell = FindFromSavedList(cell.ssid)

        # Already Saved from Setting
        if savedcell:
            savedcell.activate()
            return cell

        # First time to conenct
        else:
            if cell.encrypted:
                if password:
                    scheme = Add(cell, password)

                    try:
                        scheme.activate()

                    # Wrong Password
                    except wifi.exceptions.ConnectionError:
                        Delete(ssid)
                        return False

                    return cell
                else:
                    return False
            else:
                scheme = Add(cell)

                try:
                    scheme.activate()
                except wifi.exceptions.ConnectionError:
                    Delete(ssid)
                    return False

                return cell

    return False


def Add(cell, password=None):
    if not cell:
        return False

    scheme = wifi.Scheme.for_cell('wlan0', cell.ssid, cell, password)
    scheme.save()
    return scheme


def Delete(ssid):
    if not ssid:
        return False

    cell = FindFromSavedList(ssid)

    if cell:
        cell.delete()
        return True

    return False


if __name__ == '__main__':
    # Search WiFi and return WiFi list
    print(Search())

    # Connect WiFi with password & without password
    print(Connect('uth-open2'))
    #print(Connect('ClosedWiFi', 'password'))

    # Delete WiFi from auto connect list
    print(Delete('DeleteWiFi'))

我尝试使用其他网络,但仍然遇到相同的错误,这导致我的代码崩溃,该如何解决?

预先感谢

0 个答案:

没有答案