gpt4 book ai didi

Python-类型错误 : unorderable types: NoneType() < NoneType()

转载 作者:行者123 更新时间:2023-11-30 23:15:52 29 4
gpt4 key购买 nike

使用IPtools python 包我试图查看 IP 地址是否在特定范围内。这是我的代码:

for line in g:
org= line.split("|")[0]
ranges = ast.literal_eval(line.split('|')[1])
for range in ranges:
start,end = range
range_s = IpRange(start,end)
if '198.0.184.126' in range_s is True:
print (range_s)

这是我的文件的样子:

FOP Michail Mandryk |[('91.195.172.0', '91.195.173.255'), ('195.95.222.0', '195.95.223.255')]
Circle 3 Inc.|[('66.64.129.28', '66.64.129.31'), ('216.23.64.120', '216.23.64.120'), ('216.215.250.46', '216.215.250.47')]
a1web.com.br|[('50.116.92.89', '50.116.92.89')]
Shandong Dezhou decheng district government|[('61.133.124.64', '61.133.124.79')]
Global ICT Solutions (ShangHai) CO.,LTD|[('43.247.100.0', '43.247.103.255')]
VendorCert|[('173.1.96.112', '173.1.96.127')]
Lowell City Library|[('198.0.184.112', '198.0.184.127')]
abc|[('123.0.0.0/8' , '12.12.3.0/8')]

我收到此错误,但我找不到原因。有人可以帮忙吗?

TypeError                                 Traceback (most recent call last)
<ipython-input-59-420def563a4e> in <module>()
19
20 # print (start,end)
---> 21 range_s = IpRange(start,end)
22 # if '198.0.184.126' in range_s is True:
23 print (range_s)

/opt/miniconda3/lib/python3.4/site-packages/iptools/__init__.py in __init__(self, start, end)
158 start = _address2long(start)
159 end = _address2long(end)
--> 160 self.startIp = min(start, end)
161 self.endIp = max(start, end)
162 self._len = self.endIp - self.startIp + 1

TypeError: unorderable types: NoneType() < NoneType()

最佳答案

iptools._address2long() function如果无法解析有效的 IPv4 或 IPv6 地址,则返回 None

您的startend地址都无法解析,并且两个None上的min()函数code> 值然后失败并出现异常:

>>> min(None, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < NoneType()

三次检查您传入的地址以确保它们有效。您可以捕获此异常以打印出导致问题的值,例如:

try:
range_s = IpRange(start,end)
except TypeError:
print('Problematic addresses:', start, end)
raise

顺便说一句,您不需要需要在 if 语句中测试 is True 。这就是 if 的用途。通过比较链接,该语句在任何情况下都并不意味着您所认为的含义。严格使用:

if '198.0.184.126' in range_s:
print (range_s)

当使用 is True 时,您实际上是在测试 ('198.0.184.126' in range_s) 和 (range_s is True),这永远 说实话。

关于Python-类型错误 : unorderable types: NoneType() < NoneType(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28009518/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com