执行 python3 scrypt 时,gteeing int() 的文字无效

发布于 2025-01-19 04:44:37 字数 2814 浏览 0 评论 0原文

我正在使用来自 opencommunity 的 python 脚本来获取网卡速度,该脚本在 python2.7 上运行良好,但在 python3.6 中不起作用。

下面是脚本:

import subprocess
import netifaces
import socket
import re

hst_name = (socket.gethostname())

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

def get_bondSpeed():
    spd1 = open('/sys/class/net/{}/speed' .format(Current_inf)).read().strip()
    return spd1

def get_intSpeed():
    spd = subprocess.Popen(['/sbin/ethtool', get_Inf()], stdout=subprocess.PIPE).communicate()[0]
    pat_match=re.search(".*Speed:\s+(\d+)+.*", int(spd))
    speed = pat_match.group(1)
    return speed

if get_intSpeed() == str(1000):
  print("Service Status:  System is running with 1Gbit Speed on the host",hst_name)
elif get_intSpeed() == str(10000):
  print("Service Status:  System is running with 10Gbit Speed on the host",hst_name)

elif get_bondSpeed() == str(10000):
  print("Service Status:  System is running with 10Gbit Speed on the host",hst_name, "with bond setup!")

elif get_bondSpeed() == str(1000):
  print("Service Status:  System is running with 1Gbit Speed on the host",hst_name, "with bond setup!")

elif get_bondSpeed() == str(2000):
  print("Service Status:  System is running with 2Gbit Speed on the host",hst_name, "with bond setup!")

else:
  print("Service Status:  System is not running with Gig Speed, Please check manually on the host",hst_name)
get_Inf()

尝试时出错:

raceback (most recent call last):
  File "./getSpeedInfo.py", line 36, in <module>
    if get_intSpeed() == str(1000):
  File "./getSpeedInfo.py", line 32, in get_intSpeed
    pat_match=re.search(".*Speed:\s+(\d+)+.*", int(spd))
ValueError: invalid literal for int() with base 10: b'Settings for ens192:\n\tSupported ports: [ TP ]\n\tSupported link modes:   1000baseT/Full \n\t                        10000baseT/Full \n\tSupported pause frame use: No\n\tSupports auto-negotiation:

命令输出:这是命令行的输出,

# ethtool ens192
Settings for ens192:
        Supported ports: [ TP ]
        Supported link modes:   1000baseT/Full
                                10000baseT/Full
        Supported pause frame use: No
        Supports auto-negotiation: No
        Supported FEC modes: Not reported
        Advertised link modes:  Not reported
        Advertised pause frame use: No
        Advertised auto-negotiation: No
        Advertised FEC modes: Not reported
        Speed: 10000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: off
        MDI-X: Unknown
        Supports Wake-on: uag
        Wake-on: d
        Link detected: yes

任何有关此问题的帮助将不胜感激

i'm using a python script from the opencommunity for getting thi NIC card speed, which is working fine on the python2.7 but in python3.6 its not working .

Below is the script:

import subprocess
import netifaces
import socket
import re

hst_name = (socket.gethostname())

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

def get_bondSpeed():
    spd1 = open('/sys/class/net/{}/speed' .format(Current_inf)).read().strip()
    return spd1

def get_intSpeed():
    spd = subprocess.Popen(['/sbin/ethtool', get_Inf()], stdout=subprocess.PIPE).communicate()[0]
    pat_match=re.search(".*Speed:\s+(\d+)+.*", int(spd))
    speed = pat_match.group(1)
    return speed

if get_intSpeed() == str(1000):
  print("Service Status:  System is running with 1Gbit Speed on the host",hst_name)
elif get_intSpeed() == str(10000):
  print("Service Status:  System is running with 10Gbit Speed on the host",hst_name)

elif get_bondSpeed() == str(10000):
  print("Service Status:  System is running with 10Gbit Speed on the host",hst_name, "with bond setup!")

elif get_bondSpeed() == str(1000):
  print("Service Status:  System is running with 1Gbit Speed on the host",hst_name, "with bond setup!")

elif get_bondSpeed() == str(2000):
  print("Service Status:  System is running with 2Gbit Speed on the host",hst_name, "with bond setup!")

else:
  print("Service Status:  System is not running with Gig Speed, Please check manually on the host",hst_name)
get_Inf()

Error while trying:

raceback (most recent call last):
  File "./getSpeedInfo.py", line 36, in <module>
    if get_intSpeed() == str(1000):
  File "./getSpeedInfo.py", line 32, in get_intSpeed
    pat_match=re.search(".*Speed:\s+(\d+)+.*", int(spd))
ValueError: invalid literal for int() with base 10: b'Settings for ens192:\n\tSupported ports: [ TP ]\n\tSupported link modes:   1000baseT/Full \n\t                        10000baseT/Full \n\tSupported pause frame use: No\n\tSupports auto-negotiation:

Command output: this is the output from the command line

# ethtool ens192
Settings for ens192:
        Supported ports: [ TP ]
        Supported link modes:   1000baseT/Full
                                10000baseT/Full
        Supported pause frame use: No
        Supports auto-negotiation: No
        Supported FEC modes: Not reported
        Advertised link modes:  Not reported
        Advertised pause frame use: No
        Advertised auto-negotiation: No
        Advertised FEC modes: Not reported
        Speed: 10000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: off
        MDI-X: Unknown
        Supports Wake-on: uag
        Wake-on: d
        Link detected: yes

any help on this will be much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2025-01-26 04:44:37

您收到 ValueError 是因为您尝试将二进制字符串(包含非数字)转换为 int。

要修复它,请尝试:

    pat_match=re.search(b".*Speed:\s+(\d+).*", spd)
    speed = pat_match.group(1).decode()

由于 subprocess.Popen 的结果是二进制字符串,所以我在 b".*Speed:\s+(\d+ ).*" 并删除了 spd 周围的 int()

现在pat_match.group(1)的结果又是二进制字符串,需要.decode()

You get ValueError because you are trying to convert a binary string (containing non-numeric) to int.

To fix it, try :

    pat_match=re.search(b".*Speed:\s+(\d+).*", spd)
    speed = pat_match.group(1).decode()

As the result of subprocess.Popen is binary string, so I added b in b".*Speed:\s+(\d+).*" and removed int() around spd.

Now the result of pat_match.group(1) is again binary string, .decode() is needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文