如何使用 Python 连接 Wi-Fi?

2024 年 8 月 29 日 | 阅读 6 分钟

如今,几乎不可能找到一台没有有效互联网连接的计算机。在 21 世纪,互联网已变得至关重要。人们有多种方式可以将他们的系统连接到互联网。一种是使用传统的电缆,即以太网,另一种是使用现代的无线保真系统,也称为 Wi-Fi。Wi-Fi 使我们的生活更加轻松快捷。只需单击鼠标,轻触拇指,我们几乎可以立即连接到浩瀚的数据和资源海洋。在接下来的教程中,我们将了解如何使用像 Python 这样的高级编程语言连接 Wi-Fi。

那么,让我们开始吧。

建立已知 Wi-Fi 网络连接

在接下来的部分,我们将讨论连接到先前连接过的 Wi-Fi 网络的程序。为了完成这项任务,我们将遵循下面描述的方法:

方法

我们将为这个程序遵循一个简单的过程

步骤 1:首先,我们将导入所需的库。

步骤 2:其次,我们将使用 **cmd** 命令和名为 **OS** 的 Python 库来扫描并显示所有可用的 SSID。

步骤 3:然后,我们将选择要连接的已知 Wi-Fi 网络,并等待其成功连接。

既然我们已经理解了过程,让我们开始编码部分。我们将利用一些 Windows 命令提示符的命令来访问可用 Wi-Fi 网络列表并连接到已知 Wi-Fi 网络。但是,我们如何在 Python 脚本中编写和执行 Windows 命令提示符的命令。

Python 编程语言提供了一个名为 **OS** 的内置库。该库允许用户通过 Python 脚本直接与操作系统进行通信,使用各种方法,如 **path()、getcwd()、system()** 等等。我们甚至可以使用 OS 库的函数来执行 cmd 命令。

让我们看下面的实现来理解上述陈述。

示例

输出

Interface name : Wi-Fi
There are 11 networks currently visible. 
SSID 1 : Benny
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 2 : Honest
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 3 : Sushii
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 4 : Amazon
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 5 : Unagi91
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 6 : Printer.5G
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 7 : Printer 2.4G
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 8 : Willett
    Network type            : Infrastructure
    Authentication          : WPA-Personal
    Encryption              : CCMP
SSID 9 : MARK1
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 10 : Disconnect
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 11 : MARK1_5G
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
Input Name/SSID of the Wi-Fi network we would like to connect: MARK1_5G  
Connection request was completed successfully.
If the system is not connected yet, try reconnecting to an earlier connected SSID! 

说明

在上面的代码片段中,我们使用 import 关键字导入了 **OS** 库。然后,我们使用 **os.system()** 方法来执行以下 **cmd** 命令:

命令

上述命令有助于扫描所有可用的 SSID,并以其基础设施、身份验证和加密类型以及输出的形式显示它们。然后,我们定义了一个名为 **router_name** 的变量,该变量以字符串的形式存储用户输入的 SSID 值。

然后,将此字符串变量替换为另一个 **cmd** 命令,其中我们需要提供 SSID 的名称。

命令

我们现在将成功连接到指定的 SSID。

建立新 Wi-Fi 网络连接

在接下来的部分,我们将通过几个简单的步骤讨论与新 Wi-Fi 网络建立连接的方法。此外,为了连接到新网络,通过 XML 文件将新 Wi-Fi 网络配置文件添加到系统中也很重要。这将使 Wi-Fi 网络成为一个已知的 SSID,我们可以通过以下步骤成功连接到它:

方法

步骤 1:我们将从导入所需的库,即 **OS** 库开始。

步骤 2:然后,我们将设置新 Wi-Fi 网络的 XML 配置。

步骤 3:配置设置完成后,我们将选择 Wi-Fi 网络。

步骤 4:然后,我们将把 Wi-Fi 网络的配置文件添加到系统中。

步骤 5:最后,我们将连接到 Wi-Fi 网络。

让我们在下面的示例中看看上述过程:

示例

输出

Interface name : Wi-Fi
There are 11 networks currently visible. 
SSID 1 : Benny
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 2 : Honest
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 3 : Sushii
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 4 : Amazon
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 5 : Unagi91
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 6 : Printer.5G
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 7 : Printer 2.4G
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 8 : Willett
    Network type            : Infrastructure
    Authentication          : WPA-Personal
    Encryption              : CCMP
SSID 9 : MARK1
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 10 : Disconnect
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
SSID 11 : MARK1_5G
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
Enter the Name of Wi-Fi: MARK1_5G
Enter the Password: Thereisnopassword.
Connection request was completed successfully.
If the system is not connected to this network, try connecting with the correct password!   

说明

在上面的代码片段中,我们导入了 **OS** 模块并定义了 **create_new_connection** 函数,该函数接受 **name、SSID** 和 **password** 作为参数。这些参数都是我们需要完成的字符串,以便 **config** 变量。 **config** 变量是一个字符串,允许程序员为新的 Wi-Fi 网络定义 XML 配置。

然后,我们接受用户输入的 SSID 名称和密码。然后,它们被输入到 XML 代码中,然后通过以下几行代码将其包含为配置文件:

语法

我们现在可以通过使用本教程前面使用的相同命令来连接到 Wi-Fi,就像它是一个已知网络一样连接到该网络。