执行 python3 scrypt 时为 int() 设置无效文字
gteeing invalid literal for int() while excuting a python3 scrypt
我正在使用来自 opencommunity 的 python 脚本来获得 NIC 卡速度,它在 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
如有任何帮助,我们将不胜感激
你得到 ValueError 因为你试图将二进制字符串(包含 non-numeric)转换为 int。
要修复它,请尝试:
pat_match=re.search(b".*Speed:\s+(\d+).*", spd)
speed = pat_match.group(1).decode()
由于subprocess.Popen
的结果是二进制字符串,所以我在b".*Speed:\s+(\d+).*"
中添加了b
并删除了spd
周围的int()
。
现在pat_match.group(1)
的结果又是二进制字符串,需要.decode()
。
我正在使用来自 opencommunity 的 python 脚本来获得 NIC 卡速度,它在 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
如有任何帮助,我们将不胜感激
你得到 ValueError 因为你试图将二进制字符串(包含 non-numeric)转换为 int。
要修复它,请尝试:
pat_match=re.search(b".*Speed:\s+(\d+).*", spd)
speed = pat_match.group(1).decode()
由于subprocess.Popen
的结果是二进制字符串,所以我在b".*Speed:\s+(\d+).*"
中添加了b
并删除了spd
周围的int()
。
现在pat_match.group(1)
的结果又是二进制字符串,需要.decode()
。