【问题标题】:Python: get the current channel of WiFi interfacePython:获取WiFi接口的当前通道
【发布时间】:2017-08-30 05:04:46
【问题描述】:

你好吗?

我正在尝试找到一种简单的方法来获取 wifi 接口的频道。好吧,我找到了一种方法,但它非常混乱而且一点也不简单。我已经搜索了互联网,但似乎没有人遇到同样的问题。

我是这样做的:

我采用iwlist wlan0 channel 的输出,即:

wlan0     13 channels in total; available frequencies :
          Channel 01 : 2.412 GHz
          Channel 02 : 2.417 GHz
          Channel 03 : 2.422 GHz
          Channel 04 : 2.427 GHz
          Channel 05 : 2.432 GHz
          Channel 06 : 2.437 GHz
          Channel 07 : 2.442 GHz
          Channel 08 : 2.447 GHz
          Channel 09 : 2.452 GHz
          Channel 10 : 2.457 GHz
          Channel 11 : 2.462 GHz
          Channel 12 : 2.467 GHz
          Channel 13 : 2.472 GHz
          Current Frequency:2.427 GHz (Channel 4)

您可以清楚地看到频道(目前是频道 4)。

我正在使用subprocess-module 来获取输出:

stdout = Popen('iwlist ' + iface + ' channel', shell=True, stdout=PIPE).stdout
output = stdout.read()
channel = output[-5:-3].replace(' ', '')

输出正是我想要的,但是获取的方式太复杂了。我也不想使用任何shell命令...

还有其他方法吗?提前谢谢!

【问题讨论】:

    标签: python wifi channel


    【解决方案1】:

    你可以使用python-wifi

    from pythonwifi.iwlibs import Wireless
    
    wifi = Wireless('wlan0')
    num_frequencies, channels = wifi.getChannelInfo()
    current_freq = wifi.getFrequency()
    # Only for 2.4 GHz
    print channels.index(current_freq) + 1
    

    【讨论】:

    • 这样舒服多了!谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2015-08-22
    • 2019-06-17
    相关资源
    最近更新 更多