2017-04-25 29 views
0

我有一個下面的代碼被寫入來獲得Linux上當前運行的接口的速度,但是當我運行這個腳本時它只是拋出錯誤:TypeError: execv() arg 2 must contain only stringsPython TypeError:execv()arg 2必須只包含字符串

將不勝感激任何幫助或想法。

Below is the script:

在下面腳本我創建 1 2個功能)的一個是get_Inf()這給界面的信息。 2)第二個是get_intSpeed(),它從第一個functon獲取接口名稱並將其傳遞給os命令ethtool,該命令通過正則表達式進一步解析爲僅獲取速度,如1000Mb/s

#!/grid/common/pkgs/python/v2.7.10/bin/python 
import subprocess 
import netifaces 
import re 

''' This snippet is just to get the Speed of the running interface on the System ''' 



def get_Inf(): 
    Current_inf = netifaces.gateways()['default'][netifaces.AF_INET][1] 
    return get_Inf 

def get_intSpeed(): 
    spd = subprocess.Popen(['/sbin/ethtool', get_Inf()], stdout=subprocess.PIPE).communicate()[0] 
    pat_match=re.search(".*Speed:\s+(\d+Mb/s)\s+.*", spd)  # "d" means any number of digit following the "Mb/s". 
    speed = pat_match.group(1) 
    return speed 

def main(): 
    print get_intSpeed() 


main() 

Below is the os command /sbin/ethtool which has the Speed information of the Interface along with Other Information.

[[email protected] /]# /sbin/ethtool eth0| grep Speed 
       Speed: 1000Mb/s 
[[email protected] /]# ethtool eth0 
Settings for eth0: 
     Supported ports: [ TP ] 
     Supported link modes: 10baseT/Half 10baseT/Full 
           100baseT/Half 100baseT/Full 
           1000baseT/Full 
     Supported pause frame use: No 
     Supports auto-negotiation: Yes 
     Advertised link modes: 10baseT/Half 10baseT/Full 
           100baseT/Half 100baseT/Full 
           1000baseT/Full 
     Advertised pause frame use: No 
     Advertised auto-negotiation: Yes 
     Speed: 1000Mb/s 
     Duplex: Full 
     Port: Twisted Pair 
     PHYAD: 1 
     Transceiver: internal 
     Auto-negotiation: on 
     MDI-X: Unknown 
     Supports Wake-on: g 
     Wake-on: g 
     Link detected: yes 

我使用的Python版本2.7.10。

+0

請顯示整個錯誤。您發佈的代碼不會調用'execv'。至少,不直接。 –

+0

@BryanOakley,感謝您的反饋,我意識到我正在迴應錯誤的價值觀。 – krock1516

回答

1

您的get_Inf()函數將返回return get_Inf聲明,這不是一個字符串,這就是爲什麼execv(它被subprocess.Popen調用)在抱怨。您應該從您的功能返回Current_Inf

+0

艾伯特P,謝謝你的確抓住了。雖然我意識到這一點。 – krock1516

相關問題