使用 Python 扫描 Windows 下的 Wi-Fi 网络实例演示

使用 Python 扫描 Windows 下的 Wi-Fi 网络

  • 代码实现
  • 代码解析
    • [1. 导入库](#1. 导入库)
    • [2. 解码混合编码](#2. 解码混合编码)
    • [3. 扫描 Wi-Fi 网络](#3. 扫描 Wi-Fi 网络)
    • [4. 运行函数](#4. 运行函数)

这是我当前电脑的 wifi 连接界面。

这个是运行的效果图:

代码实现

我们使用了 Pythonsubprocess 模块来调用 Windows 的内置命令 netsh ,并结合正则表达式对输出进行解析。以下是主要的代码实现:

python 复制代码
import subprocess
import re

def decode_mixed_encoding(byte_data):
    # 尝试用UTF-8解码SSID部分(匹配"SSID X : "之后的内容)
    decoded = byte_data.decode('gbk', errors='replace')  # 先整体用GBK解码
    
    # 修正SSID部分(正则匹配SSID行)
    ssid_pattern = re.compile(r'(SSID \d+ : )(.+)')
    
    def fix_ssid(match):
        prefix = match.group(1)  # 保留"SSID X : "部分
        ssid_bytes = match.group(2).encode('gbk', errors='replace')  # 将乱码还原回字节
        try:
            fixed_ssid = ssid_bytes.decode('utf-8')  # 尝试UTF-8解码
        except:
            fixed_ssid = match.group(2)  # 解码失败则保留原样
        return prefix + fixed_ssid
    
    return ssid_pattern.sub(fix_ssid, decoded)

def scan_wifi_windows():
    try:
        raw_output = subprocess.check_output(
            ["netsh", "wlan", "show", "network", "mode=Bssid"]
        )
        print(decode_mixed_encoding(raw_output))
    except subprocess.CalledProcessError as e:
        print("Error:", e)

scan_wifi_windows()

代码解析

1. 导入库

首先,我们导入了 subprocessre 库。 subprocess 库用于执行外部命令,而 re 库用于正则表达式匹配。

python 复制代码
import subprocess
import re

2. 解码混合编码

decode_mixed_encoding 函数的主要作用是解码从 netsh 命令获取的字节数据。由于命令输出可能包含多种编码格式,因此我们首先使用 GBK 解码。

python 复制代码
def decode_mixed_encoding(byte_data):
    decoded = byte_data.decode('gbk', errors='replace')

接着,我们通过正则表达式匹配以" SSID X : "开头的行。对于匹配到的 SSID 部分,我们尝试将其从 GBK 编码转换回字节,随后再尝试用 UTF-8 解码。

python 复制代码
    ssid_pattern = re.compile(r'(SSID \d+ : )(.+)')
    
    def fix_ssid(match):
        prefix = match.group(1)  # 保留"SSID X : "部分
        ssid_bytes = match.group(2).encode('gbk', errors='replace')
        try:
            fixed_ssid = ssid_bytes.decode('utf-8')
        except:
            fixed_ssid = match.group(2)  # 解码失败则保留原样
        return prefix + fixed_ssid

最后,使用 ssid_pattern.sub(fix_ssid, decoded) 来替换 SSID 部分,返回最终的解码结果。

3. 扫描 Wi-Fi 网络

scan_wifi_windows 函数调用 netsh 命令并输出结果。如果命令执行失败,则捕获异常并打印错误信息。

python 复制代码
def scan_wifi_windows():
    try:
        raw_output = subprocess.check_output(
            ["netsh", "wlan", "show", "network", "mode=Bssid"]
        )
        print(decode_mixed_encoding(raw_output))
    except subprocess.CalledProcessError as e:
        print("Error:", e)

4. 运行函数

最后,我们调用 scan_wifi_windows() 函数,开始扫描 Wi-Fi 网络。

python 复制代码
scan_wifi_windows()
相关推荐
小静AI工程实验室5 分钟前
Python 爬虫中文乱码排查:严格解码、UTF-8 BOM 与 JSON 转义的 12 项实验
字符编码·爬虫·python
天天被压力12 分钟前
【跨市场数据实战 #08】可转债折价机会怎么筛:3个接口抓比价、列表和实时盘口
java·人工智能·python
weixin1997010801617 分钟前
[特殊字符]️《从0到1搭多平台二手ERP中台:闲鱼+淘宝+京东+拼多多+Mercari统一调度》(附Python源码)
python
王国强200917 分钟前
uv 深入指南:重新理解 Python 的包管理、依赖解析与项目工程化
python
巡山小钻风来也19 分钟前
【保姆级教程】自定义数据集微调PP-OCRv6文本检测模型
python·ocr·paddlepaddle
updayday85424 分钟前
离职域账号状态变更与Ping64操作记录核对
大数据·网络·数据库·安全·智能路由器
baopixiaoz28 分钟前
BeeQuant × BeeAgent:用AI加速策略验证
大数据·人工智能·python·区块链
浩瀚地学1 小时前
deepagents学习打卡day04
python·agent
安易算力1 小时前
PUE优化工程实践:从1.5到1.2的制冷架构与气流组织改造路径
网络·python·容器·架构·kubernetes
troy1282 小时前
Codex 安全盲区:代码漏洞生成实测
windows·python·ci/cd·pycharm·django·github·fastapi