2017-10-10 35 views
0

我使用的是橙色Pi 2g IoT板,沒有圖形界面和發行版Ubuntu 16.04。該主板有一個調制解調器2G,通常可以很好地通過Python腳本將URL發送到我的Firebase應用程序,但有時連接不會建立。這是一個通過wvdial的pppd連接。如果我的調制解調器2G連接或沒有連接,我希望在硬件(脈衝LED開/關)方面有所瞭解。如何通過python腳本識別我是否有PPP連接,如果有,請打開LED指示燈?

任何人都可以幫我解決這個問題嗎?

非常感謝!

+0

有一個廣泛的,在Python包指數Python包的(https://pypi.python.org/pypi?%3Aaction=search&term=覆盆子&提交=搜索)與覆盆子pi有關。也許你找到適合你需要的東西? – Matthias

回答

0

我不知道這方面的python功能。但我建議你使用python(如果必須的話)用一個系統實用程序來分配一個進程,這個系統實用程序會給你提供網絡設備的當前狀態。你可以按照下面這一行:Calling an external command in Python並調用例如「ifconfig」。你的PPP設備應該顯示在那裏。

+0

感謝您的輸入Matthias。可以在Python中調用外部命令。我通過使用import os庫並在代碼中寫入OS.System('mycommand')來完成此操作。我可以寫例如「ifconfig」。但問題是我如何解析它,所以我可以在這個輸出中識別一個ppp連接? –

+0

您可以通過爲設備「拼湊」而逃脫。 – Matthias

0

如果您可以使用外部python軟件包:pip install netifaces。

使用此軟件包,您可以測試該接口是否存在,然後測試您是否可以訪問Google。這段代碼沒有經過測試,但應該讓你非常接近。

import netifaces 
import requests 

ppp_exists = False 
try: 
    netifaces.ifaddresses('ppp0') # this assumes that you only have one ppp instance running 
    ppp_exists = True 
except: 
    ppp_exists = False 

# you have an interface, now test if you have a connection 
has_internet = False 
if ppp_exists == True: 
    try: 
     r = requests.get('http://www.google.com', timeout=10) # timeout is necessary if you can't access the internet 
     if r.status_code == requests.codes.ok: 
      has_internet = True 
     else: 
      has_internet = False 
    except requests.exceptions.Timeout: 
     has_internet = False 

if ppp_exists == True and has_internet == True: 
    # turn on LED with GPIO 
    pass 
else: 
    # turn off LED with GPIO 
    pass 

UPDATE

可以使用

os.system('ifconfig > name_of_file.txt') 

然後可以解析這個反正你喜歡使用ifconfig的輸出記錄到一個文本文件中。以下是一種確認ppp接口是否存在的方法。

import os 
import netifaces 

THE_FILE = './ifconfig.txt' 

class pppParser(object): 
    """ 
    gets the details of the ifconfig command for ppp interface 
    """ 

    def __init__(self, the_file=THE_FILE, new_file=False): 
     """ 
     the_file is the path to the output of the ifconfig command 
     new_file is a boolean whether to run the os.system('ifconfig') command 
     """ 
     self.ppp_exists = False 
     try: 
      netifaces.ifaddresses('ppp0') # this assumes that you only have one ppp instance running 
      self.ppp_exists = True 
     except: 
      self.ppp_exists = False 
     if new_file: 
      open(the_file, 'w').close() # clears the contents of the file 
      os.system('sudo ifconfig > '+the_file) 
     self.ifconfig_text = '' 
     self.rx_bytes = 0 
     with open(the_file, 'rb') as in_file: 
      for x in in_file: 
       self.ifconfig_text += x 

    def get_rx_bytes(self): 
     """ 
     very basic text parser to gather the PPP interface data. 
     Assumption is that there is only one PPP interface 
     """ 
     if not self.ppp_exists: 
      return self.rx_bytes 
     ppp_text = self.ifconfig_text.split('ppp')[1] 
     self.rx_bytes = ppp_text.split('RX bytes:')[1].split(' ')[0] 
     return self.rx_bytes 

只需撥打pppParser()。get_rx_bytes()

+0

感謝您的意見,馬特!其實我的數據消費有限。例如,我的設備每隔幾分鐘嘗試一次請求都不可行。這會危害我的產品。我想通過一種方法來分析'ifconfig'輸出並解析是否有RX(xx.xx)數據發送。你認爲這是可能的嗎? –

+0

是的,這是可能的。我用一個簡單的文本解析器方法更新了答案。 –

相關問題