Python - TypeError: unorderable types: NoneType() < NoneType()
Python - TypeError: unorderable types: NoneType() < NoneType()
使用 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()
如果无法解析有效的 IPv4 或 IPv6 地址,iptools._address2long()
functionreturnsNone
。
你的 start
和 end
地址都无法解析,然后 min()
函数在两个 None
值上失败,你得到的例外是:
>>> 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) and (range_s is True)
,这 永远不会 为真。
使用 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()
如果无法解析有效的 IPv4 或 IPv6 地址,iptools._address2long()
functionreturnsNone
。
你的 start
和 end
地址都无法解析,然后 min()
函数在两个 None
值上失败,你得到的例外是:
>>> 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) and (range_s is True)
,这 永远不会 为真。