python - Python IP 地址模块
问题描述
我希望我能在我的 python 模块安装方面获得一些帮助,特别是在 ipaddress 模块周围。这个问题快把我逼疯了...
简而言之,我在我的 Windows 机器上编写了一个使用 ipaddress 模块的 python3 脚本。这绝对没问题。
我已将它复制到我想在其上运行它的 Linux 机器(Ubuntu 18.04),但是当我运行它时,我收到以下错误:
File "/opt/netbox-2.6.7/netbox/reports/address-parents.py", line 82, in test_aci_endpoints
if endpoint.subnet_of(summary):
AttributeError: 'IPv4Network' object has no attribute 'subnet_of'
当我查询模块时,我得到以下信息:
nbxla01lv:/opt/netbox/netbox$ pip3 show ipaddress
Name: ipaddress
Version: 1.0.23
Summary: IPv4/IPv6 manipulation library
Home-page: https://github.com/phihag/ipaddress
Author: Philipp Hagemeister
Author-email: phihag@phihag.de
License: Python Software Foundation License
Location: /home/andy/.local/lib/python3.6/site-packages
Requires:
在查看该模块的主页时,这让我有点困惑,它似乎是 python 2.7 的 3.3+ ipaddress 模块的一个端口。无论如何,版本 1.0.23 是最新版本并包含函数“subnet_of”。
此外,如果我查看 /home/andy/.local/lib/python3.6/site-packages/ipaddress.py 中的实际代码本身,我可以在代码中看到实际功能:
nbxla01lv:/home/andy/.local/lib/python3.6/site-packages$ cat ipaddress.py | grep subnet_of
if not other.subnet_of(self):
if other.subnet_of(s1):
elif other.subnet_of(s2):
def _is_subnet_of(a, b):
def subnet_of(self, other):
return self._is_subnet_of(self, other)
return self._is_subnet_of(other, self)
我确信这很简单,但任何帮助将不胜感激。
谢谢!
编辑 - 示例代码
# Query APIC for all endpoint IPs.
endpointQuery = '/api/node/class/fvIp.json'
resp = requests.get(aciBaseURL + endpointQuery, cookies=cookie, verify=False).json()
ipAddressCount = int(resp["totalCount"])
aciIPs = []
counter = 0
summary = ipaddress.ip_network(inputSummary)
while counter < ipAddressCount:
endpoint = ipaddress.ip_network(resp['imdata'][counter]["fvIp"]["attributes"]["addr"])
if endpoint.subnet_of(summary):
aciIPs.append(str(endpoint))
counter+=1
解决方案
该ipaddress
模块是标准库的一部分,所以我猜您正在导入该版本。
您还可以验证您实际导入的模块
>>> import ipaddress
>>> ipaddress.__file__
'/Users/rickard/.pyenv/versions/3.7.4/lib/python3.7/ipaddress.py'
您当前安装的 Python 中的模块很可能subnet_of
缺少该方法(看起来像 3.6)ipaddress
推荐阅读
- php - 默认显示一个 symfony 表单块,但我需要零个块
- java - 如果可以通过反射绕过访问修饰符,它们的目的是什么?
- azure-devops-rest-api - 使用 powershell 脚本获取测试套件的默认测试人员
- r - 在 R 中将数字 1000 格式化为 1k,将 1000000 格式化为 1m 等
- performance - MoviePy:减慢视频的渲染速度
- angular8 - 电子邮件和密码的正则表达式验证不起作用
- tensorflow - 在 {TF 2.0.0-beta1 上使用 tflite_convert 时出现“未知(自定义)损失函数”;Keras} 模型
- python-3.x - 一个简单的条件出了问题?
- lua - Eclipse中命令行参数中的引号?
- c# - 在 Win10 上 SourceCookifier 64 位编译出现问题