You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python获取笔记本热点已连接设备的名称

获取Windows笔记本热点已连接设备名称的Python方案

wmi、platform模块确实没法直接获取热点连接设备信息,Windows系统下可以通过调用netsh命令来实现,以下是具体实现代码:

import subprocess

def get_hotspot_connected_devices():
    try:
        # 执行netsh命令获取热点状态
        result = subprocess.check_output(
            ["netsh", "wlan", "show", "hostednetwork"],
            encoding='gbk',  # Windows命令行默认编码为gbk,避免乱码
            stderr=subprocess.STDOUT
        )
        lines = result.splitlines()
        connected_devices = []
        # 定位到已连接用户区域
        start_idx = None
        for idx, line in enumerate(lines):
            if "已连接用户" in line:
                start_idx = idx + 1
                break
        if start_idx is not None:
            # 提取设备名称(每行开头的设备名部分)
            for line in lines[start_idx:]:
                line = line.strip()
                if line and not line.startswith("---"):
                    # 分割出设备名称(格式通常为"设备名                    MAC地址")
                    device_name = line.split()[0]
                    connected_devices.append(device_name)
        return connected_devices
    except subprocess.CalledProcessError as e:
        # 热点未开启或命令执行失败时返回空列表
        return []

# 调用示例
devices = get_hotspot_connected_devices()
if devices:
    print("已连接设备:")
    for device in devices:
        print(f"- {device}")
else:
    print("无已连接设备或热点未开启")

说明:

  1. 该代码仅适用于Windows系统,依赖Windows自带的netsh命令
  2. 需要确保Python脚本以管理员权限运行,否则可能无法获取热点信息
  3. 若热点未开启,netsh命令会返回错误,代码会捕获并返回空列表
  4. 输出的设备名称是设备在热点中显示的名称,部分设备可能仅显示MAC地址前缀(取决于设备设置)

内容的提问来源于stack exchange,提问作者ahmedkhaled177

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 23:15:34