How do you determine if an IP address is private, in Python?(在 Python 中,如何确定 IP 地址是否是私有的?)
问题描述
在 Python 中,确定 IP 地址(例如,'127.0.0.1'
或 '10.98.76.6'
)是否在 私有网络?代码听起来并不难写.但是可能存在比立即明显更多的边缘情况,并且需要考虑 IPv6 支持等.是否有现有的库可以做到这一点?
In Python, what is the best way to determine if an IP address (e.g., '127.0.0.1'
or '10.98.76.6'
) is on a private network? The code does not sound difficult to write. But there may be more edge cases than are immediately apparent, and there's IPv6 support to consider, etc. Is there an existing library that does it?
推荐答案
查看 IPy 模块.如果有一个函数 iptype()
似乎可以做你想做的事:
Check out the IPy module. If has a function iptype()
that seems to do what you want:
>>> from IPy import IP
>>> ip = IP('127.0.0.0/30')
>>> ip.iptype()
'PRIVATE'
这篇关于在 Python 中,如何确定 IP 地址是否是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中,如何确定 IP 地址是否是私有的?


基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01