执行 python3 scrypt 时,gteeing int() 的文字无效
我正在使用来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到 ValueError 是因为您尝试将二进制字符串(包含非数字)转换为 int。
要修复它,请尝试:
由于
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 :
As the result of
subprocess.Popen
is binary string, so I addedb
inb".*Speed:\s+(\d+).*"
and removedint()
aroundspd
.Now the result of
pat_match.group(1)
is again binary string,.decode()
is needed.