Reference:
Learn IP Address Concepts With Python's ipaddress Module – Real Python
In Python, you can manipulate IP Address as an object, using Python’s ipaddress
module. This module is built-in and will not require downloading in any IDEs.
The IPv4 Address typically has the following form: n1.n2.n3.n4/c
→ n1, n2, n3, n4 are called octets, each octet ranges from 0 to 255; c is called CIDR (Notation, ranging from 0 to 32. The above form is called the decimal form of the IPv4 Address. Alternative form: xxx.xxx.xxx.xxx/c
IPv4 Address in Binary form: bbbbbbbb.bbbbbbbb.bbbbbbbb.bbbbbbbb/c
Each octet contributes to 8 bits, which is 8 binary values.
→ That makes the total number of possible IP Addresses: 2^32 = 4,294,967, 296
Generate an IPv4 address with Python:
# IPv4 Address with Python
from ipaddress import IPv4Address
ip = IPv4Address("192.168.12.10")
print(ip)
Output:
192.168.12.10
Compare two IPv4 Addresses.
# Compare two IPv4 Addresses
from ipaddress import IPv4Address
ip1 = IPv4Address("192.168.12.10")
ip2 = IPv4Address("220.168.13.134")
print(ip1 < ip2)
print(ip1 > ip2)
Output: