[英]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.