【问题标题】:How do I get Python to know what Wifi the user is connected to?如何让 Python 知道用户连接到什么 Wifi?
【发布时间】:2016-01-18 13:15:24
【问题描述】:

我今年 14 岁,请原谅我的 Python 知识。我正在尝试使用这样的 if/else 语句使这个程序仅在我在学校时(在学校的 Wifi 上)运行:

if ontheschoolwifi:
     Keep running the program
else:
     close the program because im not at school and wont need it

我想知道如何让 python 知道如何获取它所连接的 wifi。 提前感谢您的帮助:)

【问题讨论】:

  • 哪个操作系统?编程语言只是语言,但 wifi 是一个硬件接口,通常由操作系统处理,非常松散地说,操作系统负责公开一个应用程序编程接口 (API),其中编写的程序是一种可以用来查询/交互的语言(比如 Python)。
  • @metatoaster 我正在运行 Windows 10。
  • 看看这个线程及其链接线程:Associating my Windows computer to a wifi AP with Python

标签: python python-2.7 module wifi


【解决方案1】:
import subprocess

if "SchoolWifiName" in subprocess.check_output("netsh wlan show interfaces"):
    print "I am on school wifi!"

【讨论】:

    【解决方案2】:

    对于 Mac 操作系统,使用 os 模块查询机场。 "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I" 然后,查看学校分配给SSID 的名称。 其他操作系统应该是类似的。

    【讨论】:

    • OP 没有使用 Mac OS。
    【解决方案3】:

    这里有一些实际有效的代码,其他答案在 Windows 上对我不起作用...

    import subprocess
    
    wifi = subprocess.check_output(['netsh', 'WLAN', 'show', 'interfaces'])
    data = wifi.decode('utf-8')
    if "school_wifi_name" in data:
        print("connected to speccific wifi")
    else:
        print("not connected")
    

    【讨论】:

      【解决方案4】:

      这将帮助您获取网络名称。

      import subprocess
      
      subprocess_result = subprocess.Popen('iwgetid',shell=True,stdout=subprocess.PIPE)
      subprocess_output = subprocess_result.communicate()[0],subprocess_result.returncode
      network_name = subprocess_output[0].decode('utf-8')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多