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 地址是否是私有的?
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
