Jacob Gabrielson Jacob Gabrielson - 1 year ago 133
Python Question

How do you determine if an IP address is private, in Python?

In Python, what is the best way to determine if an IP address (e.g.,

) 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?

Answer Source

Check out the IPy module. If has a function iptype() that seems to do what you want:

>>> from IPy import IP
>>> ip = IP('')
>>> ip.iptype()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download