版本:Python 2.7.12 操作系统:Ubuntu 16.04.1 内核:4.4.0-31-通用#50-Ubuntu SMP Arch:ppc64le
netaddr.IPNetwork失败: netaddr.core.AddrFormatError:无效的IPNetwork'9.2.0.0 / 16'
我有一个功能:
def innet2(ip, net):
print("ip: "+ip, "net: " + net)
ip = IPAddress(ip).value
print("ipnew: " + str(ip))
network = IPNetwork(net)
print("network-first: " + str(network.first))
print("network-last: " + str(network.last))
if ip >= network.first and ip <= network.last:
return True
else:
return False
如果我在程序开头调用此函数以进行调试 它正确执行:
if __name__ == "__main__":
FLAGS(sys.argv)
startSSH()
print ("service ssh finished")
isParamReady = False
hostsStr = ""
isChef = False
for i in range(0, 100):
time.sleep(20)
print("test: " + str(innet("9.2.132.186", "9.2.0.0/16")))
print("test2: " + str(innet2("9.2.132.186", "9.2.0.0/16")))
print("test2: " + str(innet2("10.1.3.2", "9.2.0.0/16")))
isParamReady, hostsStr, isChef = **getHostIpStr()**
break
if (isParamReady is True and isChef is True):
execCommand(hostsStr)
else:
waitOrExit()
当从getHostIPStr()调用它时,它会生成AddrFormatError
def getHostIpStr():
hostsStr = "-host "
isChef = False
print("namespace= " + namespace)
print("FLAGS.job_name= " + FLAGS.job_name)
print("FLAGS.network= " + FLAGS.network)
ps_pods = v1.list_namespaced_pod(namespace, label_selector="job="
+ FLAGS.job_name)
job = v1batch.list_namespaced_job(namespace, label_selector="job="
+ FLAGS.job_name)
worker_num = job.items[0].spec.parallelism
items = ps_pods.items
print("items= ", len(items))
print("worker_num= " + str(worker_num))
if (len(items) < worker_num):
return False, "", False
for i in range(0, len(items)):
podIp = items[i].status.pod_ip
print("podIp:" + podIp)
print("localIp:" + localIp)
if (i == 0 and podIp == localIp):
isChef = True
hostIPs = getIp(podIp)
net = FLAGS.network
print("len of Ips: " + str(len(hostIPs)))
for j in range(0, len(hostIPs)):
print("j: " + str(j), "hostIPs[j]: " + hostIPs[j],
"network: " + FLAGS.network)
ip = hostIPs[j];
res = innet2(ip, net)
if (res is True):
podIp = hostIPs[j]
hostsStr = hostsStr + podIp
break
if (i < len(items)-1):
hostsStr = hostsStr + ","
return True, hostsStr, isChef
答案 0 :(得分:0)
I discovered my problem.
I was passing a quoted network address and IPNetwork was failing when trying to convert the octet from of ip address into decimal format because the first octet had the string quote in it.