【问题标题】:How to call One function into another function in python 2.6如何在 python 2.6 中将一个函数调用到另一个函数中
【发布时间】:2017-04-23 11:37:47
【问题描述】:

我正在尝试将函数 get_ethname 调用到另一个函数 get_ethSpeed,但我无法理解如何调用它。

非常感谢您提前提供的意见。

第一个函数的输出返回网卡接口的名称 在系统上如下..

[root@tss/]# cat intDetail1.py
#!/usr/bin/python
import ethtool

def get_ethname():
    inames = ethtool.get_devices()
    inameCurr = inames[1]
    print inameCurr
    return inameCurr

def main():
    get_ethname()

main()
[root@tss /]# ./intDetail1.py
eth0

下面是我试图调用它的主要代码。

 #!/usr/bin/python
    import ethtool
    import subprocess

    def get_ethname():
        inames = ethtool.get_devices()
        inameCurr = inames[1]
        print inameCurr
        return inameCurr

    def get_ethSpeed():
        spd = subprocess.popen("['ethtool',  'get_ethname']", stdout=subprocess.PIPE).communicate()[0]
        print spd
        return spd

    def main():
        get_ethname()
        get_ethSpeed()

    main()

当我运行上面的代码时,它给出了以下错误。

  File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

我的目标是获取系统上主要运行的接口名称,然后使用 linux 系统实用程序ethtool 来确定网卡的速度,它告诉接口的速度:

[root@tss /]# /sbin/ethtool eth0| grep Speed
              Speed: 1000Mb/s

ethtool eth0 的输出外观如下:

[root@tss /]# 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

【问题讨论】:

  • 文件名及其树形结构/关系是什么?
  • popen 需要参数列表。你给了它一个字符串。去掉双引号应该没问题。此外,不再支持python2.6。你应该尽快转到python2.7 或者最好是python3
  • @DaniSpringer,抱歉,我无法准确回答您的问题。我只是想通过ethtool 命令接收eth0,这是linux系统命令,所以说第二个函数需要获取/sbin/ethtool eth0输出的输出。代码中没有调用文件。
  • 另外,get_ethname 需要调用你定义的函数:spd = subprocess.popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0]
  • @DaniSpringer,很高兴收到您的意见。

标签: python


【解决方案1】:

get_ethname() 没有此类设备设置:无可用数据

这仍然是与原始问题相同的问题。您正在传递一个文字字符串并期望 shell 调用 Python 函数?

除了实际的 shell 命令外,这里没有引号

spd = subprocess.Popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0]

或者,制作另一个变量

iface = get_ethname()
 # Or 
 iface = ethtool.get_devices()[1]

spd = subprocess.Popen(['/sbin/ethtool', iface], stdout=subprocess.PIPE).communicate()
return spd[0]

请注意,您仍然需要 grep(或使用 python 扫描输出)以获得“速度”

【讨论】:

  • cricket_007..... 你正确地抓住了它,虽然我已经删除了那些文字字符串,但我错过了删除我在你回答后意识到的函数周围的引号。虽然真正的酱汁仍然需要提取:) "Speed" 的东西。感谢您的投入.. 任何关于此的历史都会很棒。
  • 您可以将子进程通过管道传输到另一个使用 grep、正则表达式或循环输出的子进程
  • cricket_007 .. 感谢您的提示,还请将popen 编辑为Popen,这是您提供的代码中的拼写错误,以防有人复制粘贴。
  • 好的。顺便一提。 stackoverflow.com/questions/32712129/…
猜你喜欢
  • 2022-06-17
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多