在Python中,如何确定IP地址是否私有? [英] How do you determine if an IP address is private, in Python?

查看:247
本文介绍了在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地址是否私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆