简体   繁体   English

我如何获得 Python 以了解用户连接的 Wifi?

[英]How do I get Python to know what Wifi the user is connected to?

I'm 14, pardon my Python knowlege.我 14 岁,请原谅我的 Python 知识。 I'm trying to make this program that will only run while I'm at school (on the school's Wifi) using an if/else statement like this:我正在尝试使用如下 if/else 语句使该程序仅在我在学校时(在学校的 Wifi 上)运行:

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

I'd like to know how to let python know how to get what wifi it is connected to.我想知道如何让 python 知道如何获取它连接的 wifi。 Thank you, in advance, for your help:)预先感谢您的帮助:)

import subprocess

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

For Mac OS query the airport using os module.对于 Mac OS,使用os模块查询机场。 "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I" Then, look the name assigned to SSID by your school. "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I"然后,查看学校分配给SSID的名称。 It should be something similar for the other operating systems.对于其他操作系统,它应该是类似的。

here some code that actually works, the other answers did not work for me on Windows...这里有一些实际有效的代码,其他答案在 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")

This will help you out to get the network name.这将帮助您获取网络名称。

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')

This should be the best solution:这应该是最好的解决方案:

import subprocess

if 'SchoolWifiName' in subprocess.check_output("netsh wlan show interfaces").decode('utf-8'):
    print('I am on school wifi!')
import subprocess

print(str(subprocess.check_output(['iwgetid -r'], shell=True)).split('\'')[1][:-2])

will print the current Wi-Fi SSID将打印当前的 Wi-Fi SSID

To check if the SSID has the desired name:要检查 SSID 是否具有所需的名称:

import subprocess

if 'School_wifi_name' in str(subprocess.check_output(['iwgetid -r'], shell=True)).split('\'')[1][:-2]:
  print('I am on school Wi-Fi!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何知道django中是否连接了远程用户? - how do I know if a remote user is connected in django? 在 Python 中,如果用户通过 SSH 连接,我如何获取用户的远程 IP(他们的最后一跳)? - In Python, how do I get user's remote IP (their last hop) if they're connected over SSH? 我怎么知道用户是否通过python脚本中的ssh连接到本地计算机? - How can I know if the user is connected to the local machine via ssh in my python script? Python套接字:如何获取客户端连接的地址? - Python sockets: How do I get the address a client is connected to? 我怎么知道 Windows 中正在运行哪些 python 脚本? - How do I know what python scripts are running in Windows? 我如何知道在Python中使用哪种数据类型? - How do I know what data type to use in Python? 我如何知道连接到服务器的客户端数量,并将连接的客户端数量返回给用户? - How can I know the number of clients connected to the server and return the number of the connected clients to the user? python如何“知道”与“ in”关键字有关? - How does python “know” what to do with the “in” keyword? 如何知道我连接的麦克风是否被任何用户使用? (Python 或 Linux x84-64) - How to know if my connected microphone is being used by any user? (Python OR Linux x84-64) 用于 wifi 连接设备的 python 程序 - python program for wifi connected devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM